from django import forms
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['field1', 'field2']
@login_required # if you need it
def add_view(request):
form = MyForm()
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
the_new_entry = form.save()
return redirect() # wherever you want to go next
return render(request, 'add_template', {'form': form})> Am I correct in understanding that using ModelForm example you listed as a view does not need to be added to urls then? What is the user access page then?The add_view would need to be added to your urls.
I added the add_view to teh urls.py
from oneidentry.forms import HardwareidFormThanks for the help Collin...I need to read through more of the documentation it seems! Going through the chapter am I correct in understanding that using ModelForm example you listed as a view does not need to be added to urls then? What is the user access page then? Sorry this is all really still new to me and I do not understand why they are running. I went back to review the forms documentation here: https://docs.djangoproject.com/en/1.7/topics/forms/ so is this two different ways to do the same thing?
| Request Method: | GET |
|---|---|
| Request URL: | http://127.0.0.1:8000/hardwareid/get_barcode.html |
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
The current URL, hardwareid/get_barcode.html, didn't match any of these.
(1048, "Column 'pub_date' cannot be null")
| Request Method: | POST |
|---|---|
| Request URL: | http://127.0.0.1:8000/get_barcode.html |
| Django Version: | 1.7 |
| Exception Type: | IntegrityError |
| Exception Value: | (1048, "Column 'pub_date' cannot be null") |
| Exception Location: | c:\users\win7va~1\appdata\local\temp\easy_install-kwxg3e\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\connections.py in defaulterrorhandler, line 36 |
| Python Executable: | C:\Python27\python.exe |
| Python Version: | 2.7.8 |
| Python Path: | ['C:\\hwdb\\mysite', 'C:\\Python27\\lib\\site-packages\\setuptools-5.7-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg', 'C:\\windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] |
| Server time: | Thu, 16 Oct 2014 13:46:37 -0700 |
C:\Python27\lib\site-packages\django\core\handlers\base.py in get_responseC:\hwdb\mysite\oneidentry\views.py in get_barcodeC:\Python27\lib\site-packages\django\forms\models.py in saveC:\Python27\lib\site-packages\django\forms\models.py in save_instanceC:\Python27\lib\site-packages\django\db\models\base.py in saveC:\Python27\lib\site-packages\django\db\models\base.py in save_baseC:\Python27\lib\site-packages\django\db\models\base.py in _save_tableC:\Python27\lib\site-packages\django\db\models\base.py in _do_insertC:\Python27\lib\site-packages\django\db\models\manager.py in manager_methodC:\Python27\lib\site-packages\django\db\models\query.py in _insertC:\Python27\lib\site-packages\django\db\models\sql\compiler.py in execute_sqlC:\Python27\lib\site-packages\django\db\backends\utils.py in executeC:\Python27\lib\site-packages\django\db\backends\utils.py in executeC:\Python27\lib\site-packages\django\db\backends\mysql\base.py in executeC:\Python27\lib\site-packages\django\db\backends\mysql\base.py in executec:\users\win7va~1\appdata\local\temp\easy_install-kwxg3e\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\cursors.py in executec:\users\win7va~1\appdata\local\temp\easy_install-kwxg3e\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\connections.py in defaulterrorhandlerfrom django.utils import timezone
class MyModel(models.Model):
pub_date = models.DateField(default=timezone.now)