Currently, I am writing up a bit of a product-based CMS as my first project.

Here is my question. How can I add additional data (products) to my Product model?

I have added '/admin/products/add' to my urls.py, but I don't really know where to go from there. How would i build both my view and my template? Please keep in mind that I don't really know all that much Python, and i am very new to Django

How can I do this all without using this existing django admin interface.


Follow the Django tutorial for setting up the "admin" part of an application. this will allow you to modify your database.

Django Admin Setup

alternatively, you can just connect directly to the database using the standard tools for whatever database type you are using

Matthew Watson

I have been through that, but that only really provides information on how to modify (existing) data. What I am after is a way to add entries to a model through a custom view, not the supplied Django admin.

This isn't a forum, you should add a comment to your original question, the answer you're replying to or edit your original question

the topic is covered in django tutorials

+4  A: 

You will want to wire your URL to the Django create_object generic view, and pass it either "model" (the model you want to create) or "form_class" (a customized ModelForm class). There are a number of other arguments you can also pass to override default behaviors.

Sample URLconf for the simplest case:

from django.conf.urls.defaults import *
from django.views.generic.create_update import create_object

from my_products_app.models import Product

urlpatterns = patterns('',
    url(r'^admin/products/add/$', create_object, {'model': Product}))

Your template will get the context variable "form", which you just need to wrap in a <form> tag and add a submit button. The simplest working template (by default should go in "my_products_app/product_form.html"):

<form action="." method="POST">
  {{ form }}
  <input type="submit" name="submit" value="add">

Note that your Product model must have a get_absolute_url method, or else you must pass in the post_save_redirect parameter to the view. Otherwise it won't know where to redirect to after save.

Carl Meyer