How To Configure Html Form To Work With Django Models?
I am trying to configure HTML formto work with Django Models rather than using built-in Forms in the framework. I have made the form using Html below and have pasted the code for M
Solution 1:
You forgot to define name in each of your input
<formid="formHook"action="/addbook/"method="post">
{% csrf_token %}
<pstyle="font-family:Courier New">
Name <inputtype="text"name="name"placeholder="Name of the book"></input></p><pstyle="font-family:Courier New">
Author <inputtype="text"name="author"placeholder="Author of the book"></input></p><pstyle="font-family:Courier New">
Status
<selectname="status"><optionvalue="Read">Read</option><optionvalue="Unread">Unread</option></select></p><inputtype="submit"id="booksubmit"value="Submit"></input></form>
Your addbook must be in the views.py not in your models.py. You don't have to define templates/index.html in your render, it is understood in your settings
defaddbook(request):
if request.method == 'POST':
name = request.POST['name']
author = request.POST['author']
bookInfo.objects.create(Name = name, Author=author)
return render(request, 'index.html', {'Name': name, 'Author': author})
main urlconf
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'project_name.views.index'),
url(r'^addbook/$', 'project_name.views.addbook'),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Solution 2:
You need to add name attributes to your html form then process the form submission in the view. Something like -
from django.http import HttpResponseRedirect
from django.shortcuts import render
defbook_view(request):
if request.method == 'POST':
name = request.POST['name']
author = request.POST['author']
book = BookInfo(name=name, author=author)
if book.is_valid():
book.save()
return HttpResponseRedirect('your_redirect_url')
else:
return render(request, 'your_form_page.html')
Have a look at the docs on the request.POST dictionary.
However you really would be better off doing this with a django ModelForm -
classBookForm(ModelForm):
classMeta:
model = BookInfo # I know you've called it bookInfo, but it should be BookInfo
then in your view -
from django.http import HttpResponseRedirect
from django.shortcuts import render
defbook_view(request, pk=None):
if pk:
book = get_object_or_404(BookInfo, pk=pk)
else:
book = BookInfo()
if request.method == 'POST':
form = BookForm(request.POST, instance=book)
if form.is_valid():
book = form.save()
return HttpResponseRedirect('thanks_page')
else:
form = BookForm(instance=book)
return render(request, 'your_form_page.html', {'form': form)
And your_form_page.html
can be as simple as -
<formaction="{% url book_view %}"method="post">{% csrf_token %}
{{ form.as_p }}
<inputtype="submit"value="Submit" /></form>
Have a look at the docs on working with forms.
Post a Comment for "How To Configure Html Form To Work With Django Models?"