Django Forms vs Templates - python

Sorry the inconvenient, but I am extremely newbie on Django. I imagined that Django would create the forms for me if I use forms.py, I would not need to create an input tag on template, so I created a test template only, since I created the forms.py. However, running my code I was told that my view didn't return an HttpResponse object, and I suspect it was due to my template having only text on it. Could you help me creating a template to have my forms working ? We can use the example posted above. I will paste it bellow:
def create_post(request):
if request.method == 'POST':
form = CreatePostForm(request.POST)
if form.is_valid():
my_model = form.save()
return redirect('/posts/')
else:
form = CreatePostForm()
c = {'form' : form}
return render(request,'create_post.html',c)

You should return render() instead of HttpResponse:
from django.shortcuts import render
def create_a_my_model(request):
...
return render(request, 'template.html', c)
template.html can be very primitive:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button>Save</button>
</form>
Also note that the good practice is to redirect to some page after the post request. This will prevent the double submit. So the whole code of your view will be:
from django.shortcuts import redirect, render
def create_a_my_model(request):
if request.method == 'POST':
form = MyModelForm(request.POST)
if form.is_valid():
my_model = form.save()
return redirect(my_model) # if the model has `get_absolute_url()`
else:
form = MyModelForm()
return render(request, 'template.html', {'form': form})
If you model doesn't have the get_absolute_url() method the you can redirect to any other url: return redirect('/success/page/')

Related

Store name value with Django sessions for another view

The error that Django throws at me when I fill in the form is: 'GetSessionName' object has no attribute 'name' and I have never worked with sessions before so I don't know how to solve this problem. The idea of the site is to have a small Login screen where you type your name then move to the chat.
views.py
from django.shortcuts import render, redirect
from django.utils import timezone
from .models import *
from .forms import Chat, GetSessionName
def chat(request):
Messages = Message.objects.all().order_by('-created')
form = Chat()
if request.method == "POST":
form = Chat(request.POST)
if form.is_valid():
form.save()
return redirect ("chat")
name = request.session['name']
context = {"Message": Messages, "form": form, 'name': name}
return render(request, 'chat/chat_messages.html', context)
def login(request):
form = GetSessionName()
if request.method == "POST":
form = GetSessionName(request.POST)
if form.is_valid():
request.session['name'] = form.name
return redirect('chat')
context={'form': form}
return render(request, 'chat/chat_login.html', context)
forms.py
from django import forms
from django.forms import ModelForm
from .models import Message
from .models import *
class Chat(forms.ModelForm):
class Meta:
model = Message
fields = "__all__"
class GetSessionName(forms.Form):
name = forms.CharField(max_length=30)
chat_login.html
{% extends 'chat/base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{form}}
</form>
{% endblock content %}
you clearly right used sessions but the problem is getting field name in dicts of forms.
You just should add 'cleaned_data'
def login(request):
form = GetSessionName()
if request.method == "POST":
form = GetSessionName(request.POST)
if form.is_valid():
request.session['name'] = form.cleaned_data['name']
return redirect('chat')
context={'form': form}
return render(request, 'chat/chat_login.html', context)
Best regards

Django UserCreationForm neither gives error message nor registering user

I am beginner to python Django. And trying build an posting article website with the help of tutorials. I got stuck at UserCreationForm. I have created a form using UserCreationForm, but when I am submitting the form I am not able to neither submit the form nor getting any error message on the page.
My views.py code
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.template.context_processors import csrf
from django.contrib.auth.forms import UserCreationForm
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
args = {}
args.update(csrf(request))
args['form'] = UserCreationForm()
print args
return render_to_response('register.html', args)
def register_success(request):
return render_to_response('register_success.html')
register.html
{% extends "base.html" %}
{% block content %}
<h2>Register</h2>
<form action="/accounts/register/" method="post"> {% csrf_token %}
{{form}}
<input type="submit" value="Register"/>
</form>
{% endblock %}
register_success.html
{% extends "base.hml" %}
{% block content %}
<h2>You have registered!</h2>
<p>Click Here to login again</p>
{% endblock %}
The problem is that you are always creating a blank form.
args['form'] = UserCreationForm()
This means that you do not see any errors for POST requests when the form is invalid.
Instead, you should only create the blank form for GET requests.
from django.shortcuts import render
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
else:
form = UserCreationForm()
args = {'form': form}
return render(request, 'register.html', args)
Note that I have simplified the view by using render instead of the obsolete render_to_response. That means you don't need to handle csrf manually.
You can use Django Generic Views, specifically CreateView, it will make your life a lot easier. You can use it like so:
from django.views.generic import CreateView
class CreateUserView(CreateView):
template_name = 'register.html'
form_class = UserCreationForm
success_url = '/accounts/register_success'
Add this to your urls.py and you are good to go:
from mysite.views import CreateUserView
# add this url pattern
url(r'^sign_up/$', CreateUserView.as_view(), name='signup'),

Django-form not rendered in HTML

The view that **renders** is
def codequestion(request, question_id):
question = Question.objects.get(pk=question_id)
return render(request, 'polls/codequestion.html', {'question': question})
the view that is called on submission is
def codequestion_evaluate(request, question_id):
form = CodeForm()
print request.method
if request.method == 'POST':
form = CodeForm(request.POST)
if form.is_valid():
data = form.cleaned_data
return HttpResponse("Your code is %s" % data['solution'])
else:
return HttpResponse("not valid")
else:
return HttpResponse("Error")
class
from django import forms
class CodeForm(forms.Form):
solution = forms.CharField(widget=forms.Textarea)
template
<form action="{% url 'codequestion_evaluate' question.id %}" method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit" />
</form>
I do not get the form field display in the HTML page, I can only see the submit button.
The view that is suppose to show the unfilled form doesn't create the form object at all. It should create a form object and pass it to the template, like this:
def codequestion(request, question_id):
question = Question.objects.get(pk=question_id)
form = CodeForm()
return render(request, 'polls/codequestion.html', {'question': question, 'form': form})
But better yet you should follow the pattern described in Django documentation. To do this you should:
Delete the codequestion. All actions (displaying the unfilled form, displaying a submitted form with errors, processing a correctly submitted form) will be handled by a single view.
Configure your url routing so codequestion_evaluate view handles the page showing the unfilled form.
Change codequestion_evaluate so it follows the pattern:
def codequestion_evaluate(request, question_id):
if request.method == 'POST':
form = CodeForm(request.POST)
if form.is_valid():
# The form has been submitted and is valid
# process the data and redirect to a "thank you" page
data = form.cleaned_data
return HttpResponseRedirect('/thanks/')
else:
# just display an empty form
form = CodeForm()
# you can optionally add 'question' if you need it in your template
question = Question.objects.get(pk=question_id)
return render(request, 'polls/codequestion.html', {'form': form, 'question': question})
form refers to a variable in your context data, since you haven't included it in the context data, it can't find it so there isn't anything to render, you need to include it.
def codequestion(request, question_id):
question = Question.objects.get(pk=question_id)
return render(request, 'polls/codequestion.html',
{'question': question, 'form': CodeForm()})
Try changing
class CodeForm(forms.Form):
to
class CodeForm(forms.ModelForm):
I faced same problem but it got resolved from this.
The recent distributions of django don't have widgets included. So:
pip install django-widgets
should do the trick.

displaying django form validation errors for ModelForms

I often find myself using a ModelForm in views to display and translate views. I have no trouble displaying the form in the template. My problem is that when I am working with these, the forms often don't validate with the is_valid method. The problem is that I don't know what is causing the validation error.
Here is a basic example in views:
def submitrawtext(request):
if request.method == "POST":
form = SubmittedTextFileForm()
if form.is_valid():
form.save()
return render(request, 'upload_comlete.html')
return render(request, 'failed.html')
else:
form = SubmiittedTextFileForm()
return render(request, 'inputtest.html', {'form': form})
I know that the form is not validating because I am redirected to the failed.html template, but I never know why .is_valid is false. How can I set this up to show me the form validation errors?
Couple of things:
You are not taking the POST being sent to the POST.
To see the error message, you need to render back to the same template.
Try this:
def submitrawtext(request):
if request.method == "POST":
form = SubmittedTextFileForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'upload_comlete.html')
else:
print form.errors #To see the form errors in the console.
else:
form = SubmittedTextFileForm()
# If form is not valid, this would re-render inputtest.html with the errors in the form.
return render(request, 'inputtest.html', {'form': form})
I faced the same annoying problem and solved it by returning the form.errors.values() back with HttpResponse. Here is the code:
#csrf_exempt
def post(request):
form = UserForm(request.POST)
if form.is_valid():
return HttpResponse('All Good!')
else:
return HttpResponse(form.errors.values()) # Validation failed
In my case it returned:
<ul class="errorlist"><li>This field is required.</li></ul>
<ul class="errorlist"><li>This field is required.</li></ul>
<ul class="errorlist"><li>This field is required.</li></ul>
<ul class="errorlist"><li>This field is required.</li></ul>
It doesn't provide much information, but it is enough to give you an idea.

Form from Model with File Upload

I'm trying to mimic the admin interface for the Photologue app on the front end. To achieve this, I have thus far created a bit of code in the view:
def galleryuploader(request):
GalleryFormSet = modelformset_factory(GalleryUpload)
if request.method == 'POST':
formset = GalleryFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
# do something. ... do what?
else:
formset = GalleryFormSet()
return render_to_response("cms_helper/gallery_upload.html", {
"formset": formset,
})
and a template:
<form method="post" action="">
{{ formset }}
<input type="submit" />
</form>
I'm using django's "form from models" method for generating this front-end form.
The problem: when I try to upload a file (because I am uploading photos to a photo gallery), and hit submit, it returns with a form error telling me that a required field was missing (the file).
I think I am not checking the request for any files, but even if I were to, I'm not quite sure how to. Here's some documentation about file uploads, but I haven't been able to decipher it yet.
If you have any suggestions about how to make this upload form work, I'd be veryyy happy to hear them. Thanks in advance!
Add the enctype="multipart/form-data" attribute to your form tag. Also you'll need to actually do something with the uploaded files. Here's the example from the django docs:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
# you'll need to loop through the uploaded files here.
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
def handle_uploaded_file(f):
destination = open('some/file/name.txt', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
(See the comment about halfway through)

Categories

Resources