Skip to content Skip to sidebar Skip to footer

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

<form id="formHook" action="/addbook/" method="post">
    {% csrf_token %}
    <p style="font-family:Courier New">
        Name <input type="text" name="name" placeholder="Name of the book"></input>
    </p>

    <p style="font-family:Courier New">
        Author <input type="text" name="author" placeholder="Author of the book"></input>
    </p>

    <p style="font-family:Courier New"> 
        Status
        <select name="status">
            <option value="Read">Read</option>
            <option value="Unread">Unread</option>
        </select>
    </p>
    <input type="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

def addbook(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

def book_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 -

class BookForm(ModelForm):
    class Meta:
        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

def book_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 -

<form action="{% url book_view %}" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="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?"