I have my models.py
class Schedule(models.Model):
name = models.CharField(max_length=255)
date_from = models.DateField('')
date_to = models.DateField('', null=True)
desc = models.TextField(blank=True, null=True)
here my views.py
class Schedule(CreateView):
fields = ()
model = models.Schedule
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
return super(ModelFormMixin, self).form_valid(form)
and here my template.html
{{form.as_p}}
this form only can do 1 time input. however I need to perform 3 times input in single form with different name & date (in my case).
and form maybe look like
{{form.as_p}}
{{form.as_p}}
{{form.as_p}}
I check on documentation theres bulk_create can do multiple input in single run but i dont have idea how to deal with my template.html
A demo for you:
views.py
from django import forms
from django.shortcuts import render, HttpResponse
from .models import Schedule
class ScheduleForm(forms.ModelForm):
class Meta:
model = Schedule
fields = "__all__"
def multicreate(request):
if request.method == "POST":
forms = [
ScheduleForm(dict(name=n, date_from=df, date_to=dt, desc=ds))
for n, df, dt, ds in zip(
request.POST.getlist("name"),
request.POST.getlist("date_from"),
request.POST.getlist("date_to"),
request.POST.getlist("desc"),
)
]
if all(forms[i].is_valid() for i in range(len(forms))):
for form in forms:
form.save()
return HttpResponse(
f"success to create {len(forms)} Schedule instances."
)
else:
forms = [ScheduleForm() for _ in range(3)]
return render(request, "create.html", {"forms": forms})
models.py
from datetime import date
from django.db import models
class Schedule(models.Model):
name = models.CharField(max_length=255)
date_from = models.DateField("date from", default=date.today)
date_to = models.DateField("date to", default=date.today)
desc = models.TextField(blank=True, null=True)
def __str__(self):
return self.name or self.__class__.__name__
template
<form method='post'>{% csrf_token %}
{% for form in forms %}
{{ form.Meta.model }} {{ forloop.counter }}<br>
{{ form.as_p }}
-------------------------------------------<br>
{% endfor %}
<input type='submit', value='OK'>
</form>
Related
I am working on a movie website with Django and I'm trying to add user comments. Only the registered users should be allowed to comment. I have the following so far:
models.py:
class Comment(models.Model):
movie = models.ForeignKey(Movie, related_name = "comments", on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete = models.CASCADE)
content = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.movie.title, self.name)
forms.py
class CommentForm(forms.ModelForm):
content = forms.CharField(label ="", widget = forms.Textarea(
attrs ={
'class':'form-control',
'placeholder':'Comment here!',
'rows':5,
'cols':50
}))
class Meta:
model = Comment
fields =['content']
views.py
class MovieDetailsView(generic.DetailView):
model = Movie
template_name = "details.html"
def comment(request, id):
movie= Movie.objects.get(id)
if request.method == 'POST':
cf = CommentForm(request.POST or None)
if cf.is_valid():
content = request.POST.get('content')
comment = Comment.objects.create(movie = movie, user = request.user, content = content)
comment.save()
return redirect(movie.get_absolute_url())
else:
cf = CommentForm()
context ={
'comment_form':cf,
}
return render(request, 'details.html', context)
details.html
<form method="POST" action="#" class="form">
{% csrf_token %}
{{comment_form.as_p}}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
<textarea id="text" name="text" class="form__textarea" placeholder="Add comment"></textarea>
<button type="button" class="form__btn">Send</button>
</form>
The MovieDetailsView displays the details page of the movie and has a comment section. However, when I submit the comment, it simply displays a white page and this link: http://127.0.0.1:8000/details/1# . The comment is not saved on the database and I can't seem to find what the issue is. I am also following the example from this link.
Thanks in advance!
You can combine FormMixin with DetailView - Using FormMixin with DetailView.
from django.urls import reverse
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from django.http import HttpResponseForbidden
class MovieDetailsView(FormMixin, DetailView):
model = Movie
template_name = "details.html"
form_class = CommentForm
def get_success_url(self):
return reverse('movie_detail', kwargs={'pk': self.object.pk})
# or
# return self.object.get_absolute_url()
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.movie = self.object
form.save()
return super().form_valid(form)
In the post() method, we first check if the user is logged in:
if not request.user.is_authenticated:
return HttpResponseForbidden()
In template:
<form method="POST" class="form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</form>
You can remove the action attribute, the form will be submitted to the same page.
I'm trying to incorporate Select2 into my django form -- specifically ModelSelect2MultipleWidget -- so that the user can associate multiple event objects to another model (like CheckboxSelectMultiple). The associated models are:
from django.contrib.auth import get_user_model
from django.db import models
class F4Events(models.Model):
name = models.CharField(max_length=300)
handling_type = models.ManyToManyField(WaferHandlingType)
def __str__(self):
return self.name
class ToolPm(models.Model):
ceid = models.ForeignKey(CEID, on_delete=models.CASCADE)
name = models.CharField(max_length=250)
wafer_handling = models.BooleanField(default=True)
wafer_based = models.BooleanField(default=False)
frequency = models.FloatField(blank=True, null=True)
handling_type = models.ManyToManyField(WaferHandlingType, blank=True)
user_edited = models.BooleanField(default=False)
pm_f4_events = models.ManyToManyField('F4Events', blank=True)
def __str__(self):
return str(self.name) if self.name else ''
class Meta:
ordering = ('ceid', 'name')
My forms.py file is:
from django import forms
from .models import Entity, ToolPm, CEID, PDL, F4Events, WaferHandlingType
from django_select2 import forms as s2forms
class F4EventWidget(s2forms.ModelSelect2MultipleWidget):
search_fields = [
'name__icontains',
]
attrs = {'data-placeholder': 'Please Choose F4 Events'}
class PMForm(forms.ModelForm):
class Meta:
model = ToolPm
fields = ['wafer_handling', 'wafer_based', 'handling_type', 'pm_f4_events']
widgets = {'handling_type': forms.CheckboxSelectMultiple,
'pm_f4_events': F4EventWidget
}
my view.py is:
from datetime import datetime, timedelta
from django.db.models import Sum
from django.http import JsonResponse, HttpResponseRedirect
from django.http.response import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.views import View
from django.views.generic import ListView
from django_filters.views import FilterView
from pages.filter import CeidFilter, PmRunDateFilter
from tools.forms import EntityForm, PMForm, CEIDForm, PDLAddForm
from tools.models import CEID, Entity, F4Events, ToolPm, WaferHandlingType, PmRunDate
from django.urls import reverse
class UserCeidPMUpdate(View):
template_name = 'tools/update_pm_attributes.html'
def get(self, request, username, pk, pm_id):
pm = ToolPm.objects.get(pk=pm_id)
form = PMForm(request.GET or None, instance=pm)
return render(request, self.template_name, {'form': form, 'pm': pm, })
def post(self, request, username, pk, pm_id):
pm = ToolPm.objects.get(pk=pm_id)
pm.user_edited = True
pm.save()
form = PMForm(request.POST or None, instance=pm)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('pm_view', kwargs={'username': username, 'pk': pm.ceid.id}))
return render(request, self.template_name, {'form': form, 'pm': pm, })
lastly, my .html file is:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post">
<p>Process: {{ pm.ceid.process }}</p>
<p>CEID: {{ pm.ceid.ceid }}</p>
<p>Name: {{ pm.name }}</p>
<p>Frequency: {{ pm.frequency }}{% if pm.wafer_based %} wafers {% else %} days {% endif %}</p>
<p>Score: {{ pm.score }}</p>
{% csrf_token %}
{{ form }}
<input type="submit" class="btn btn-success" value="Update PM"><button type="button" class="btn btn-primary">Go Back</button>
</form>
{% endblock content %}
If I switch the pm_f4_events widget to a CheckboxSelectMultiple or a ModelSelect2Widget, the code works.
However, when I try to use a ModelSelect2MultipleWidget, the form continually tries to load but cannot and eventually just times out.
There are ~5000 items within the F4Events model, so that may have something to do with it. Any help to point me in the right direction would be greatly appreciated!
You can empty the pm_f4_events field's choices in the __init__ method:
class PMForm(forms.ModelForm):
class Meta:
model = ToolPm
fields = ['wafer_handling', 'wafer_based', 'handling_type', 'pm_f4_events']
widgets = {
'handling_type': forms.CheckboxSelectMultiple,
'pm_f4_events': s2forms.ModelSelect2MultipleWidget(
model=F4Events,
search_fields=["name__icontains",],
attrs = {'data-placeholder': 'Please Choose F4 Events'}
)
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
choices_F4Events = []
pm_f4_events_field = self.fields["pm_f4_events"]
# selected values
selected_F4Events = self.data.getlist("pm_f4_events") or self.initial.get("pm_f4_events")
if selected_F4Events:
# check of selected values not object
if not isinstance(selected_F4Events[0], F4Events):
selected_F4Events = pm_f4_events_field.queryset.filter(pk__in=selected_F4Events)
choices_F4Events = [(item.pk, item.name) for item in selected_F4Events]
# assign selected values or empty list
pm_f4_events_field.choices = choices_F4Events
Has been tested on my server, works well.
I'm trying to make a simple django app which is like a todo app, I want to add the percentage of task completed.
Here's my model.py
from django.db import models
from django.urls import reverse
class Task(models.Model):
title = models.CharField(max_length=200)
create_time = models.DateTimeField(auto_now_add=True)
complete_time = models.DateTimeField(blank=True, null=True)
status = models.BooleanField(default=False)
def __str__(self):
return self.title
and here's the template file
<form method="POST" action="/">
{% csrf_token %}
{{form}}
<input class="btn submit" type="submit" name="save">
</form>
{% for task in tasks %}
{% if task.status == True %}
<strike>{{task}}, {{task.complete_time}}</strike>
{% else %}
{% endif %}
{% endfor %}
and this is views.py file
def list(request):
queryset = Task.objects.order_by('complete_time','complete_time')
if request.method =='POST':
form = TaskForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
context = {
'tasks':queryset,
'form':form,
}
return render(request, 'tasklist.html', context)
use a class-based view
in views.py
from django.views.generic.list import ListView
class list(ListView):
model = Task
template_name = 'tasl_list.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
context['get_percentage_done'] = Task.objects.filter(status = True).count() * 100 / Task.objects.all().count()
return context
in your template
{{get_percentage_done}}
I am trying to add comment to an Ad posted by a user. When I press the comment button, it is showing the error.
Here's my models.py
from django.db import models
from django.core.validators import MinLengthValidator
from django.contrib.auth.models import User
from django.conf import settings
class Ad(models.Model):
title = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
)
price = models.DecimalField(max_digits=7, decimal_places=2, null=True)
text = models.TextField()
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#picture
picture = models.BinaryField(null=True, blank=True, editable=True)
content_type = models.CharField(max_length=256, null=True, help_text='The MIMEType of the file')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
comments = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Comment', related_name='comments_owned')
#show up in the admin list
def __str__(self):
return self.title
class Comment(models.Model) :
text = models.TextField(
validators=[MinLengthValidator(3, "Comment must be greater than 3 characters")]
)
ad = models.ForeignKey(Ad, on_delete=models.CASCADE)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
# Shows up in the admin list
def __str__(self):
if len(self.text) < 15 :
return self.text
return self.text[:11] + ' ...'
Here's my forms.py
# commentForm
from django import forms
from ads.models import Ad, Comment
from django.core.files.uploadedfile import InMemoryUploadedFile
from ads.humanize import naturalsize
from django.core import validators
from django.core.exceptions import ValidationError
...
class CommentForm(forms.Form):
comment = forms.CharField(required=True, max_length=500, min_length=3, strip=True)
class Meta:
model = Comment
fields = ['text']
Here's my views.py
from ads.models import Ad, Comment
from ads.owner import OwnerListView, OwnerDetailView, OwnerCreateView, OwnerUpdateView, OwnerDeleteView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.views import View
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy, reverse
from django.http import HttpResponse
from ads.forms import CreateForm, CommentForm
class AdDetailView(OwnerDetailView):
model = Ad
def get(self, request, pk):
a = Ad.objects.get(id=pk)
comments = Comment.objects.filter(ad=a).order_by('-updated_at')
comment_form = CommentForm()
context = {'ad': a, 'comments':comments, 'comment_form':comment_form}
return render(request, 'ads/ad_detail.html', context)
....
class CommentCreateView(LoginRequiredMixin, View):
def post(self, request, pk):
ad = get_object_or_404(Ad, id=pk)
comment = Comment(text=request.POST['comment'], owner=request, ad=ad)
comment.save()
return redirect(reverse('ads:ad_detail', args=[pk]))
class CommentDeleteView(OwnerDeleteView):
model = Comment
def get_success_url(self):
ad = self.object.ad
return reverse('ads:ad_detail', args=[ad.id])
And finally my templates ad_detail.html
{% if user.is_authenticated %}
<br clear="all"/>
<p>
{% load crispy_forms_tags %}
<form method="post" action="{% url 'ads:ad_comment_create' ad.id %}">
{% csrf_token %}
{{ comment_form|crispy }}
<input type="submit" value="Submit">
<input type="submit" value="All Ads" onclick="window.location.href='{% url 'ads:all' %}'; return false;">
</form>
</p>
{% endif %}
{% for comment in comments %}
<p> {{ comment.text }}
({{ comment.updated_at|naturaltime }})
{% if user == comment.owner %}
<i class="fa fa-trash"></i>
{% endif %}
</p>
{% endfor %}
After clicking on the submit button, it's supposed to be taking to ads:all but instead throwing an error.
In case you also want to check urls.py
urlpatterns = [
path('', views.AdListView.as_view()),
path('ads', views.AdListView.as_view(), name='all'),
...
#comments
path('ad/<int:pk>/comment', views.CommentCreateView.as_view(), name='ad_comment_create'),
path('comment/<int:pk>/delete', views.CommentDeleteView.as_view(success_url=reverse_lazy('ads')), name='ad_comment_delete'),
]
You should be using ModelForm instead of Form here.
class CommentForm(forms.ModelForm):
comment = forms.CharField(required=True, max_length=500, min_length=3, strip=True)
class Meta:
model = Comment
fields = ['text']
And change your view
class CommentCreateView(LoginRequiredMixin, View):
def post(self, request, pk):
form = CommentForm(request.POST)
ad = get_object_or_404(Ad, id=pk)
comment = form.save(commit=False)
comment.ad = ad
comment.owner = request.user
comment.save()
return redirect('ads:ad_detail', ad.pk)
In you CommentCreateView, your owner is recieving request that was the reason you were shown that error. So:
comment = Comment(text=request.POST['comment'], owner=request.user, ad=ad)
I'm doing an application and now I need to make an evaluation that users can take, my problem is that I want to use a formset to list the questions with respective choices, I know this can be done with using formsets but not the way to get it done. Following is my code:
# models.py
class Evaluation(models.Model):
"""
An evaluation is for a session.
Each session should have an evaluation
"""
session = models.OneToOneField(
Session,
related_name='evaluation',
verbose_name=u'Sesión'
)
class Meta:
verbose_name = u'Evaluación'
verbose_name_plural = u'Evaluaciones'
def __unicode__(self):
return u'Evaluación de la sesion {0}'.format(
self.session.name
)
class Question(models.Model):
"""
A question inside of an evaluation
"""
evaluation = models.ForeignKey(
Evaluation,
verbose_name=u'Evaluación',
related_name='questions'
)
question_type = models.CharField(
max_length=20,
verbose_name=u'Tipo de respuesta',
choices=QUESTION_TYPES
)
sentence = models.CharField(
max_length=255,
verbose_name=u'Pregunta'
)
position = models.IntegerField(
default=0,
verbose_name=u'Posición'
)
class Meta:
verbose_name = u'Pregunta'
verbose_name_plural = u'Preguntas'
ordering = ['position', 'sentence']
def __unicode__(self):
return self.sentence
class Choice(models.Model):
question = models.ForeignKey(
Question,
related_name='choices',
verbose_name=u'Pregunta'
)
sentence = models.CharField(
max_length=255,
verbose_name=u'Posible respuesta'
)
position = models.IntegerField(
default=0,
verbose_name=u'Posición'
)
class Meta:
verbose_name = u'Posible respuesta'
verbose_name_plural = u'Posibles respuestas'
ordering = ['position', 'sentence']
def __unicode__(self):
return self.sentence
----
# forms.py
from django.forms.models import inlineformset_factory
from models import Evaluation, Question
AnswerFormSet = inlineformset_factory(
Evaluation, Question, exclude=('question',),
extra=0, can_delete=False
)
----
# views.py
#login_required()
def session_evaluation(request, course_slug, session_position):
data = {}
course = get_object_or_404(Course, slug=course_slug)
session = Session.objects.filter(course=course).get(position=session_position)
evaluation = get_object_or_404(Evaluation, session=session)
if request.method == 'POST':
formset = AnswerFormSet(request.POST, instance=evaluation)
if formset.is_valid():
formset.save()
print 'formset valid...'
else:
formset = AnswerFormSet(instance=evaluation)
data['course'] = course
data['session'] = session
data['formset'] = formset
return render(request, 'courses/session_evaluation.html', data)
----
# template.html
<form id="evaluation" method="post" role="form">
{% csrf_token %}
{{ formset.management_form }}
<ul class="evaluation">
{% for form in formset.forms %}
<li>
{{ form.instance }}
{{ form.instance.choices.all }}
</li>
{% endfor %}
</ul>
<div class="form-group clearfix nmb">
<button type="submit" class="btn btn-primary pull-right">Enviar respuestas</button>
</div>
</form>
As you can see I have the models well written but from the forms all the code is only an experiment and I really don't know the way to do it.
What I'm getting in the template with this code is the question sentence and the list of choices, something like:
[question sentence here]? [<Choice: [choice #1 here]>, <Choice: [choice #2 here]>]
What is the clean and easy way to do it with formsets?
What I want to get is a list of questions with the respective list of choices with radio buttons because this is an evaluation.
Should not it be {{ form.as_p }} or {{ form.as_table }}? The instance is already created in the view, so need not be used in the template.