0


Have some trouble with displayed form via web.
I'm using Django 1.6.2
In forms.py

from django import forms

class ContactForm(forms.Form):
    pk_post = forms.CharField(max_length=1, required=False)
    user_post = forms.CharField(max_length=20, required=False)
    description_post = forms.CharField(max_length=200, required=False)

In views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse
from report.models import Main, Data
from report.forms import ContactForm
from django.template import RequestContext, loader

def index(request):
    mainc = Data.objects.all()
    template = loader.get_template('report/index.html')
    context = RequestContext(request, {
       'mainc': mainc,
       })
    return HttpResponse(template.render(context))

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        pk_post = form.cleaned_data['pk_post']
        user_post = form.cleaned_data['user_post']
        description_post = form.cleaned_data['description_post']
        p = Main.objects.get(pk=pk_post)
        p.data_set.all()
        p.data_set.create(user=user_post, description=description_post, data_date=timezone.now())

In index.html i'm added next code:

<form action="" method="post">{% csrf_token %}
        <table>
            {{ form.as_table }}
        </table>
        <input type="submit" value="Submit">
    </form>

But when I load index.html via browser I can see only "Submit" button. Table from {{ form.as_table }} not generated.

1 Answer 1

1

This is because you are not sending the form in the context:

def index(request):
    mainc = Data.objects.all()
    template = loader.get_template('report/index.html')
    context = RequestContext(request, {
       'mainc': mainc,
       'form' : ContactForm() 
       })
    return HttpResponse(template.render(context))

Consider using render() instead of HttpResponse

Also, from the code you have shown, there is an issue with def contact(): method too, I am assuming you have not posted the whole code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.