I'm trying make a comment section for my Q&A project.I made the model for comment , the form part in question_detail.html an , also the QuestionCommentForm() in form.py .
model.py
class QuestionComment(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
question =
models.ForeignKey(Question,on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add= True)
body = models.CharField(max_length=200, null= True ,
blank = True, default ='')
def __str__(self):
return str(self.body)
forms.py
{%if c_form%}
<form method="POST" action= "{% usl 'blog:question-commet' question.id >{% csrf_token %}
{{c_form.media}}
{{ c_form.as_p}}
<button type = "submit" , name = "question_id", value = "{{question.pk}}", class ="btn btn-secondary btn-sm">submit comment</button>
views.py
#api_view(['POST','GET'])
def question_comment(request, *args, **kwargs):
form = QuestionCommentForm()
print('finction comment started'*20)
if request.method == 'POST':
c_form = QuestionCommentForm(request.POST)
if c_form.is_valid():
new_comment = c_form.save(commit=False)
new_comment.refresh_from_db()
c_form.instance.user = request.user
question = Question.objects.get(id = request.POST.get('question_id')
new_comment.question = question
new_comment.bldy =c_form.cleaned_data.get('body')
new_comment.save()
context['c_form'] = c_form
return render(request, 'blog/question_detail.html',context)
class QuestionDetail(DetailView):
template_name = 'blog/question_detail.html'
model = Question
context_object_name = 'question'
count_hit = True
def get_queryset(self):
return Question.objects.filter(id = self.kwargs['pk'])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
c_form = QuestionCommentForm()
context = super(QuestionDetail,self).get_context_data(**kwargs)
self.obj= get_object_or_404(Question, id = self.kwargs['pk'])
self.object.save()
self.object.refresh_from_db()
answers = Answer.objects.filter (question_id = self.obj.id).order_by('-created_date')
liked =self.obj.like.filter(id =self.request.user.id).exists()
print('liked in class question not checked still' *10)
comments= QuestionComment.objects.filter(question = self.kwargs['pk'])
context['comments']= comments
context["answers"]=answers
context["liked "] = liked
context['c_form'] = c_form
return context
def post(request, *args, **kwargs):
print('post started'*100)
c_form = QuestionCommentForm()
c_form = QuestionCommentForm(request.POST)
if c_form.is_valid():
new_comment = c_form.save(commit=False)
new_comment.refresh_from_db()
new_comment.user = request.user
new_comment.question = c_form.cleaned_data.get('question_id')
new_comment.bldy =c_form.cleaned_data.get('body')
new_comment.save()
context['c_form'] = c_form
else:
c_form= QuestionCommentForm()
return render(request, 'blog/question_detail.html',context)
url.py
...
path('question-comment/<int:pk>/', question_comment, name = 'question-comment'),
]
in my view, first I tried to use a another function to handle the comment, got no result and made a def post()
in the class QuestionDetial() , and still the form shows up but when I type something and hit the button , it refresh the page and nothing saves . I have already saved a comment using admin and it appears in the question-detail page. used print to find the bug, but it seems the post() in class and the question_comment() not being recall. searched a lot but no answer. (BTW I get no error except the NoReverseMatch that I fixed)
You never save the model object (not by the form, nor by the view). Furthermore the name of the method is 'POST', not 'Post':
#api_view(['POST','GET'])
def question_comment(request, *args, **kwargs):
form = QuestionCommentForm()
print('finction comment started'*20)
if request.method == 'POST':
c_form = QuestionCommentForm(request.POST)
if c_form.is_valid():
c_form.instance.user = request.user
c_form.save()
context = {'c_form': c_form }
return render(request, 'blog/question_detail.html',context)
ok , the problem was that I had two form-tags in question_detail template and buy adding comment form in the end of the template had 3 , but the problem was when I pressed the submit comment button the terminal showed this message
"POST /create-like/blog/answer/18/ HTTP/1.1" 302 0
which was the form I had made for like function .(while my url was question-comment)
I accidentally forgot to close the like-form tag in template. so all I needed was putting a
</form>
after the form block .
thank's Willem Van Onsem. it solved my post method error.
and these were very helpful
Proper way to handle multiple forms on one page in Django
How can I build multiple submit buttons django form?
Related
I have practically finished the microblogging application and I want to change to class base views now. I read about generic views, but it looks like each of them has specific properties. In my index view I display two models and two forms, so I created IndexView (View) and put the get and post functions there and it works, but it doesn't look like there are any benefits of class views, so I'm probably doing something wrong. Currently, several views repeat a lot of code, and I thought that class views would solve this problem. I would like an example of how such a view should be written as class view with reusable get.
class IndexView(View):
def get(self, request, *args, **kwargs):
post_form = AddPostForm()
comment_form = AddCommentForm()
if request.user.is_authenticated:
logged_user = CustomUser.objects.get(id=request.user.id)
blocked_users = logged_user.blocked.all()
posts = Post.objects.exclude(author__in=blocked_users)
print(blocked_users)
posts = posts.order_by('-pub_date')
else:
posts = Post.objects.all()
posts = posts.order_by('-pub_date')
comments = Comment.objects.all()
comments = comments.order_by("-pub_date")
return render(request, 'mikroblog/index.html', {'posts': posts, 'comments': comments,
'post_form': post_form, 'comment_form': comment_form})
def post(self, request, *args, **kwargs):
if request.method == 'POST':
post_form = AddPostForm(request.POST)
comment_form = AddCommentForm(request.POST)
if post_form.is_valid():
new_post = post_form.save(commit=False)
new_post.author = request.user
new_post.tags = ''
content = ''
for word in new_post.content_post.split():
if '#' in word:
new_post.tags += f"{word} "
content += f"{word} "
else:
content += f"{word} "
new_post.content_post = content
new_post.save()
for word in content.split():
if '#' in word:
print(word)
user_to_notificate = CustomUser.objects.get(username=word[1:])
new_notification = TalkAbout()
new_notification.where = new_post
new_notification._from = new_post.author
new_notification.to = user_to_notificate
new_notification.sended = False
print(new_notification)
new_notification.save()
post_form = AddPostForm()
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.author = request.user
new_comment.save()
comment_form = AddCommentForm()
return HttpResponseRedirect(reverse('index'))
Use class-based views and override the 'post' and 'get' methods if you need to.
Follow this link:
Django CBVs documentation
I tried creating model form to add new record to my models in django, but I got this error:
The view post.views.add_job_resume didn't return an object. It returned None instead.
this is my files(Notice that in this file I have more codes than what I write here):
view.py
def add_job_resume(request):
if request.method=="POST":
form = AddJobForm(request.POST)
if form.is_valid():
job_resume = form.save(commit=False)
job_resume.user= request.user
job_resume.save()
return redirect('view_job_resume')
else:
form = AddJobForm()
return render(request, 'education/job_research_education_records/job_resume/add_job_resume.html', {'form': form})
forms.py
class AddJobForm(ModelForm):
class Meta:
model = JobRecord
fields = [
'title',
'explains',
'post',
'organization',
'time_start',
'time_end',
'upload_url',
]
models.py
class JobRecord(models.Model):
user = models.ForeignKey(User, on_delete=models.PROTECT)
title = models.TextField(max_length=250, blank=False)
explains = models.TextField(max_length=500, blank=True)
post = models.TextField(max_length=100, blank=False)
organization = models.TextField(max_length=100, blank=False)
time_start = models.TextField(max_length=100)
time_end = models.TextField(max_length=100)
upload_url = models.FileField(upload_to='job-resume-files/')
def __str__(self):
return self.title
add_job_resume.html
<form method="post" >
{% csrf_token %}
{{form.as_p }}
<button type="submit" class="btn btn-info">add</button>
</form>
urls.py
urlpatterns = [
path('edu/resume/job/', views.view_job_resume, name='view_job_resume'),
path('edu/resume/job/add', views.add_job_resume, name='add_job_resume')]
I search a lot for this error but I can't solve that! What is going on?
This has nothing to do with the redirect(..) itself. You just forgot a code path. It is possible that the method is indeed POST, but that the form is not valid. So the condition in the first if is satisfied, but the condition in the second if is not. Right now, your view will return nothing (hence return None) for this situation.
It is common to simply rerender the template, but now with the invalid form, such that the form can show the errors, like:
def add_job_resume(request):
if request.method=="POST":
form = AddJobForm(request.POST)
if form.is_valid():
job_resume = form.save(commit=False)
job_resume.user= request.user
job_resume.save()
return redirect('view_job_resume')
else:
form = AddJobForm()
# Not placed under the else
return render(
request,
'education/job_research_education_records/job_resume/add_job_resume.html',
{'form': form}
)
Notice that the render is not put under the else part. Indeed regardless whether the method is POST and the form is invalid, or the method is not POST, we render the page and respond with the rendered page.
Redirecting is not the problem. The problem is that your form is not valid, and the view does not turn anything if that happens
You should unindent the last line one level, so that it catches this case.
I'm trying to populate my ModelForm with some of data that I have submitted to previous HTML page which is also ModelForm.
I just want to pass it to another form so it doesn't have to be written twice.
I've tried couple solutions from stackoverflow but they are 6+ years old, kinda outdated and also couldnt come up with solution from django docs https://docs.djangoproject.com/en/2.2/topics/forms/
I have two models, which have same fields which are name and boxid
I need to pass it from first input to second(to populate it).
forms.py
class NewCashierForm(forms.ModelForm):
class Meta:
model = Cashier
fields = ("cashier_company", "cashier_dealer", "cashier_name", "cashier_boxid", "cashier_type", "cashier_package", "cashier_otheritem", "cashier_otheritemserial", "cashier_length", "cashier_promotion", "cashier_amount", "cashier_paymenttype")
labels = {"cashier_company":('Firma'), "cashier_dealer": ('Diler'), "cashier_name":('Ime i prezime'), "cashier_boxid":('Box ID'), "cashier_type":('Tip'), "cashier_package":('Paket'), "cashier_otheritem":('Drugi uredjaj'), "cashier_otheritemserial":('SBU'), "cashier_length":('Dužina'), "cashier_promotion":('Promocija'), "cashier_amount":('Iznos'), "cashier_paymenttype":('Nacin uplate')}
exclude = ['cashier_published']
def save(self, commit=True):
cashier = super(NewCashierForm, self).save(commit=False)
if commit:
cashier.save()
return cashier
class NewPersonForm(forms.ModelForm):
class Meta:
model = Person
fields = {"person_name", "person_adress", "person_phone", "person_boxid"}
labels = {"person_name":('Ime i prezime'), "person_adress":('Adresa'), "person_phone":('Telefon'), "person_boxid":('Box ID')}
def save(self, commit=True):
person = super(NewPersonForm, self).save(commit=False)
if commit:
person.save()
return person
views.py
def addcashier(request):
if request.method == 'GET':
form = NewCashierForm()
else:
form = NewCashierForm(request.POST)
if form.is_valid():
fs = form.save(commit=False)
fs.user = request.user
fs.save()
return redirect('/byauthor')
return render (request, 'main/addcashier.html', {'form':form})
def addperson(request):
if request.method == 'GET':
form = NewPersonForm()
else:
form = NewPersonForm(request.POST)
if form.is_valid():
fs = form.save(commit=False)
fs.user = request.user
fs.save()
return redirect('/addcashier')
return render (request, 'main/addperson.html', {'form':form})
addperson.html and addcashier.html
{% extends "main/base.html" %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button class="btn" type="submit">Unos i dodavanje pretplate</button>
</form>
<input type="button" value="Otkazi unos" onclick="window.history.back()" />
{% endblock %}
Any help and/or hint is appreciated.
To prepopulate the form, you need to pass an argument initial={} when initializing your form for the GET call. Since you are passing data from one view to another, you should use sessions.
def addperson(request):
if request.method == 'GET':
form = NewPersonForm()
else:
form = NewPersonForm(request.POST)
if form.is_valid():
fs = form.save(commit=False)
fs.user = request.user
fs.save()
request.session["person_form"] = request.POST.dict() #save the form as a dict in request.sessions
return redirect('/addcashier')
return render (request, 'main/addperson.html', {'form':form})
Then in your second view, use this data from sessions to initialize the form.
def addcashier(request):
if request.method == 'GET':
# get the form data from the request.session
form_data = request.session.pop('person_form', {})
box_id = form_data.get("person_boxid")
name = form_data.get("person_name")
form = NewCashierForm(initial={"cashier_name":name, "cashier_boxid":box_id}) # initialize the form with the data
else:
form = NewCashierForm(request.POST)
if form.is_valid():
fs = form.save(commit=False)
fs.user = request.user
fs.save()
return redirect('/byauthor')
return render (request, 'main/addcashier.html', {'form':form})
as title indicates I'm trying to send the name of a category user created by the current user on every page. My initial attempt was simply
{% if user.is_authenticated == category.author %}
{{category.name}}
{% endif %}
but this only displays the category in a certain page, while I want to display this in the navbar which I have it included for the every page. So I thought I should do category = models.foreignkey('category') in my user model but got told I should set a queryset in a template context processor. which I'm not sure it's the best way to do.
Can someone please direct me how I should do such matter?
here's my code
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
description = models.TextField()
author = models.ForeignKey(settings.AUTH_USER_MODEL)
and in my views.py
#login_required
def add_category(request):
if not request.user.is_superuser and Category.objects.filter(author=request.user).exists():
return render(request,'main/category_already_exists.html')
if request.method == 'POST':
category = Category(author=request.user)
form = CategoryForm(request.POST, request.FILES, instance=category)
if form.is_valid():
form.save(commit=True)
return redirect('category', category_name_url=category.name)
else:
form = CategoryForm()
context = {
"form":form
}
return render(request, 'main/add_category.html',context)
and this is my simplified category view
def category(request, category_name_url):
category_name = decode_url(category_name_url)
category = Category.objects.get(name=category_name)
context = {
"category":category,
}
return render(request, "main/category.html", context)
and this is my model for my user
from userena.models import UserenaBaseProfile
class MyProfile(UserenaBaseProfile):
user = models.OneToOneField(User, unique=True, verbose_name=_('user'), related_name='my_profile')
Perhaps you should write your own context processor and include it in settings. Link to docs https://docs.djangoproject.com/en/dev/ref/templates/api/#context-processors
Definition from Django docs:
"Context processors are functions that receive the current HttpRequest as an argument and return a dict of data to be added to the rendering context.
Their main use is to add common data shared by all templates to the context without repeating code in every view."
You can add this information in request.session. You can do this like this:
Suppose your login view is this:
def login(request):
# Do login stuff
if user.is_active():
request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user)] # add to the session
return redirect('/somepage')
Display this data in every page like this:
{% for c in request.session.categories %}
{{ c }}
{% endfor %}
And update the category list every time the a new category is added like this:
#login_required
def add_category(request):
if not request.user.is_superuser and Category.objects.filter(author=request.user).exists():
return render(request,'main/category_already_exists.html')
if request.method == 'POST':
category = Category(author=request.user)
form = CategoryForm(request.POST, request.FILES, instance=category)
if form.is_valid():
form.save(commit=True)
request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user)]
return redirect('category', category_name_url=category.name)
else:
form = CategoryForm()
context = {
"form":form
}
return render(request, 'main/add_category.html',context)
So I'm building a basic Q&A site-- Each topic has a series of questions associated with it, and each question has multiple answers associated with it.
I'm creating the user input for questions and they have to associated with a topic. This is the questions model
#models.py
class Question(models.Model):
movie = models.ForeignKey(Movie, blank=True, null=True)
question_text = models.CharField(max_length = 1000)
question_detail = models.CharField(max_length = 5000, blank = True, null = True)
q_pub_date = models.DateTimeField(auto_now_add = True)
q_author = models.ForeignKey(User)
class QuestionForm(ModelForm):
def save(self, user = None, force_insert = False, force_update = False, commit = True):
q = super(QuestionForm, self).save(commit = False)
q.q_author = user
if commit:
q.save()
return q
class Meta:
model = Question
exclude = ('movie', 'q_author', 'q_pub_date')
This is the URL conf
#urls.py
url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question'),
Now here is the view
#views.py
def add_question(request, movie_id):
if request.method == "POST":
form = QuestionForm(request.POST, request.FILES)
#QuestionForm.movie = Movie.objects.get(pk = movie_id)
if form.is_valid():
form.save(user = request.user)
return HttpResponseRedirect("/home/")
else:
form = QuestionForm()
return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request))
This is the HTML code
#add_question.html
<h1> Add Question: {{ user.username }}</h1>
<form action = "" method = "post">{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value = "Ask" />
<input type = "hidden" name = "next" value = "{{ next|escape }}" />
</form>
In the view, the commented out line is what I added to the view to try and auto save the model. When adding a question, the URL has the ID of the movie it is associated with, and my thought is to take that ID and then plug it into the ForeignKey to identify which movie is associated with the question. However, when I use my code, it changes all of the Questions' movie associations to the current movie instead of just changing that specific question's movie association. Without the code, it doesn't associate a Movie with the Question at all. How do I fix this?
Use this:
#views.py
def add_question(request, movie_id):
if request.method == "POST":
form = QuestionForm(request.POST, request.FILES)
if form.is_valid():
question = form.save(user = request.user)
question.movie = Movie.objects.get(pk = movie_id)
question.save()
return HttpResponseRedirect("/home/")
else:
form = QuestionForm()
return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request)
For question asked in comment
You should avoid using absolute URLs in views or templates. Consider a scenario, where you decide to change home URL from /home/ to /myhome/. You will have to edit it where ever you have used them. It is always better to name the urls (docs):
# URL Conf
url(r'^home/$', 'home_view', name="home_url"),
url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question', name="add_question_url"),
url(r'^home/(?P<movie_id>\d+)/$', 'movie_view', name="movie_url"),
The name argument act as an unique identifier to your actual URLs
Now in you views:
from django.core.urlresolvers import reverse
def some_view(request):
...
return HttpResponseRedirect(reverse('home_url'))
Now what ever change you make to the URL (say /home/ to /myhome/ makes no effect to the view as long as the name argument has the same value in the URL conf.
If you wish to pass parameters (like movie_id in your case)
def some_view(request, movie_id):
...
return HttpResponseRedirect(reverse('movie_url', kwargs={'movie_id':movie_id}))
The same concept should be used in templates to avoid hard-coding URLS in templates. Please read this for more details