I have a problem with django forms, when submitting a form nothing seems to happen, even the server didn't get any response except GET request to view the form template.
here is my code for the forms.py :
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = [
"title",
"content",
"category"
]
and here is my post_form.html :
{% extends 'base.html' %}
{% block content %}
<h1>form</h1>
<form method="POST" action=".">
{% csrf_token %}
{{ form.as_p }}
</form>
<button type="submit">Create Post</button>
{% endblock content %}
and here is my handling for the form in views.py :
def post_create(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect("posts:detail", pk=post.pk)
else :
form = PostForm()
context = {
"form":form,
}
return render(request,"post_form.html", context)
forms.py
from django.forms import ModelForm
from .models import Post
class PostForm(ModelForm):
class Meta:
model = Post
fields = ["title", "content", "category"]
views.py
def post_create(request):
form = PostForm()
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
post = form.save()
return redirect("posts:detail", pk=post.pk)
return render(request,"post-form.html", {"form": form})
post-form.html
{% extends 'base.html' %}
{% block content %}
<h1>form</h1>
{% for error in form.non_field_errors %}
<article class="message is-danger alert-message">
<div class="message-body">
<p>{{ error|escape }}</p>
</div>
</article>
{% endfor %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create Post">
</form>
{% endblock content %}
in your views:
if form.is_valid():
form.commit = False
form.save()
or
if form.is_valid():
form.save(commit=false)
I suggest the latter
Don't Forget Place Your Submit Button In Form Tag !
Related
The form doesn't display in the browser. The navbar and submit button show up but no form in between. The problem must be straightforward but I haven't been able to find the issue. Thank you for your help.
views.py
def ProductCreateView(request):
if request.method == 'POST':
form = ProductForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('set_app/product_list.html'))
else:
product_form = ProductForm()
return render(request, 'set_app/product_form.html', {'product_form':product_form})
forms.py
class ProductForm(forms.Form):
class Meta():
model = models.Product
fields = ('code', 'barcode', 'name', 'description', 'brand', 'status')
product_form.html
{% extends "set_app/basic_app_base.html" %}
{% block body_block %}
<h1>
{% if not form.instance.pk %}
Create Product
{% else %}
Update Product
{% endif %}
</h1>
<form method="post">
{% csrf_token %}
{{ product_form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit">
</form>
{% endblock %}
Found the issue:
in forms.py
instead of
class ProductForm(forms.Form):
it should be
class ProductForm(forms.ModelForm):
The form submits but immediately says this field is required... although it was filled out. What am I doing wrong
In my view:
def fileupload(request):
if request.user.is_authenticated and request.user.is_staff:
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES.getlist('file_field'))
return HttpResponseRedirect('/fileupload/')
else:
form = UploadFileForm()
return render(request, 'fileupload.j2.html', {'form': form})
return HttpResponseForbidden('<h1>403 Forbidden</h1>')
with this form:
class UploadFileForm(forms.Form):
kit_number = forms.CharField(label="Kit number", max_length=100, required=True, help_text='Required.')
file_field = forms.FileField(label='Upload kit result')
and template:
{% extends "menu.j2.html" %}
{% block content %}
{% if request.user.is_authenticated and request.user.is_staff %}
<h3>File upload</h3><br><br>
<form action="/fileupload/" method="post">
{% csrf_token %}
<div class="form-group">
<table>
{{ form.as_table() }}
</table>
</div>
<input id="button" class="btn" type="submit" value="Sent">
</form>
{% else %}
You are not authorized to see this page
{% endif %}
{% endblock %}
You forgot to set the form enctype.
<form action="/fileupload/" method="post" enctype="multipart/form-data">
In my project there is an opportunity even for unauthorized users to leave a comment to a post. So, I have commentForm:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('author', 'text',)
and model:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
author = models.CharField(max_length=200)
text = models.TextField()
If user is authorized, I want to substitute to author fileld username and don't show the input on page, otherwise site visitor should fill this input himself. I've tried to write this in appropriate view:
if request.user.is_authenticated:
form = CommentForm(request.POST, initial={'author': request.user.username})
But form.is_valid is false.
Then I've tried:
if request.user.is_authenticated:
form.fields['author'] = request.user.username
During form validating appeared 'string' object has no attribute 'disabled'.
The question is how to deal with it the right way? Thanks.
post_detail.html
{% extends 'blog/base.html' %}
{% block content %}
<div class="post">
{% if post.published_date %}
<div class="date">
{{ post.published_date }}
</div>
{% else %}
<a class="btn btn-default" href="{% url 'post_publish' post_id=post.pk blog_id=blog.pk %}">Publish</a>
{% endif %}
{% if user.is_authenticated and user == post.blog.author%}
<a class="btn btn-default" href="{% url 'post_remove' post_id=post.pk blog_id=blog.pk %}">удалить</a>
<a class="btn btn-default" href="{% url 'post_edit' post_id=post.pk blog_id=blog.pk %}">редактировать</a>
{% endif %}
<h1>{{ post.title }}</h1>
<p>{{ post.text|safe|linebreaks }}</p>
</div>
<hr>
<p>New comment</p>
<form method="POST" class="post-form">{% csrf_token %}
<!-- HERE I HIDE AUTHOR FIELD FROM LOGGED IN USERS -->
{% if user.is_authenticated %}
{{ form.text }}
{{ form.text.errors }}
{% else %}
{{ form.as_p }}
{% endif %}
<button type="submit" class="save btn btn-default">Send</button>
</form>
{% for comment in post.comments.all %}
<div class="comment">
<div class="date">
{{ comment.created_date }}
<!-- {% if not comment.approved_comment %} -->
<a class="btn btn-default" href="{% url 'comment_remove' post_id=post.pk blog_id=blog.pk com_id=comment.pk %}">удалить</a>
<!-- {% endif %}-->
</div>
<strong>{{ comment.author }}</strong>
<p>{{ comment.text|linebreaks }}</p>
</div>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
{% endblock %}
views.py
from .models import Post, Comment, Blog
from django.shortcuts import render
from django.utils import timezone
from django.shortcuts import render, get_object_or_404
from .forms import PostForm, CommentForm, BlogForm
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout, login
from django.contrib.auth.models import User
def post_detail(request, blog_id, post_id):
blog = get_object_or_404(Blog, pk=blog_id)
post = get_object_or_404(Post, pk=post_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
if request.user.is_authenticated:
comment.author = request.user.username
comment.post = post
comment.save()
return redirect('post_detail', blog_id=blog_id, post_id=post.pk)
else:
form = CommentForm()
return render(request, 'blog/post_detail.html', {'post': post, 'blog': blog, 'form': form})
#login_required
def post_publish(request, blog_id, post_id):
post = get_object_or_404(Post, pk=post_id)
post.publish()
return redirect('post_detail', blog_id=blog_id, post_id=post.pk)
#login_required
def post_remove(request, blog_id, post_id):
post = get_object_or_404(Post, pk=post_id)
post.delete()
return redirect('post_list', blog_id=blog_id)
#login_required
def comment_remove(request, blog_id, post_id, com_id):
comment = get_object_or_404(Comment, pk=com_id)
comment.delete()
return redirect('post_detail', blog_id=blog_id, post_id=post_id)
#login_required
def post_edit(request, blog_id, post_id):
post = get_object_or_404(Post, pk=post_id)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('post_detail', blog_id = blog_id, post_id=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'blog/post_edit.html', {'form': form})
def post_list(request, blog_id):
blog = get_object_or_404(Blog, pk=blog_id)
posts = blog.posts.all().filter(published_date__isnull=False).order_by('published_date')
return render(request, 'blog/post_list.html', {'posts': posts, 'blog': blog})
I don't know if this is the best way to solve my problem, but it helped:
change a form value before validation in Django form
I want to create a post update/edit function but I have a problem. When I click "Update" button there is no error but there is no change. I think reason of that a mistake that I made in models.py but I can not figure it out. And here is my code
models.py
class Post(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
.
.
.
def get_update_url(self):
return reverse('post:post_update', kwargs={'slug': self.slug})
views.py
def post_update(request, slug):
if not request.user.is_authenticated():
return Http404()
post = get_object_or_404(Post, slug=slug)
form = PostForm(request.POST or None, request.FILES or None, instance=post)
if form.is_valid():
form.save()
messages.success(request, "Updated")
return HttpResponseRedirect(get_absolute_url())
context = {
'form': form
}
return render(request, "blog/post_update.html", context)
post_update.html
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1>Form</h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
{{ form.media }}
<input class="btn btn-primary" type="submit" value="Create post">
</form>
</div>
</div>
</div>
{% endblock %}
post_detail.html
//update link
<p >update</p>
urls.py
url(r'^(?P<slug>[\w-]+)/update/$', post_update, name="update"),
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('title', 'text',)
Where is my mistake? What can I do?
im trying to save all the forms from my formset and it doesnt pass the validation. here is my code
def UpdatePhysician(request,id):
physician = get_object_or_404(Physician, id=id)
SpecialtyFormset = modelformset_factory(PhysicianSpecialties, fields=('specialty',),max_num=1, labels=None, )
formset = SpecialtyFormset(queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id))
if request.method == "POST":
formset = SpecialtyFormset(request.POST,)
for form in formset:
print(form)
if (formset.is_valid()):
for form in formset:
form.save()
return HttpResponse('yesh')
else:
return HttpResponse('nope')
return render(request, 'UpdatePhysician.html', {
'formset': formset,
})
it throws me an error like this
"MultiValueDictKeyError at /physicians/3/update/
"u'form-1-id'""
EDIT
I noticed when i have just 1 object in the formset it saves the record perfectly
My template is
{% extends 'prescription_menu.html' %}
{% load bootstrap3 %}
{% load crispy_forms_tags %}
{% block css %}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}site/css/select2.css"/>
{% endblock %}
{% block title %}Physicians{% endblock title %}
{% block container %}
{% csrf_token %}
<form method="POST">
<div class="row">
<div class="col-md-offset-2 col-md-8">
{{ formset.management_form }}
{% for form in formset %}
{% crispy form %}
{% endfor %}
<input type="submit" name="Save" value="Save" class="btn btn-primary" id="submit-id-save">
</div>
</div>
</form>
{% endblock container %}
I noticed that when i saved just 1 record it pass the validation, so it wasnt the formset, was something in post, so I realized that when each form renders the first one follow the layout from the form crispy layout, but the next one renders wrapped on form tags, so i made a special layout class at the bottom of my view to gives layout to my formset and is the following
class SpecialtyFormsetHelper(FormHelper):
def __init__(self, *args, **kwargs):
super(SpecialtyFormsetHelper, self).__init__(*args, **kwargs)
self.form_tag = False
self.render_required_fields = True
and the final code is the following:
def UpdatePhysician(request,id):
physician = get_object_or_404(Physician, id=id)
SpecialtyFormset = modelformset_factory(PhysicianSpecialties, fields=('specialty',),max_num=1,)
formset = SpecialtyFormset(queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id))
helper = SpecialtyFormsetHelper()
if request.method == "POST":
formset = SpecialtyFormset(request.POST,)
for form in formset:
print(form)
if formset.is_valid():
for form in formset:
form.save()
return HttpResponse('yesh')
else:
return HttpResponse('nope')
return render(request, 'UpdatePhysician.html', {
'formset': formset,
'helper': helper,
})
and i just add helper to the template render
like this:
{{ formset.management_form }}
{% for form in formset %}
{% crispy form helper %}
{% endfor %}
Thank You Alasdair for your help :), you were very helpful