Can't manage to display form from my view - django

I'm trying to create a page where admins can upload some files using some FileField. The problem is that I can't manage to display any field from my form, I must be missing something important but I can't find out what, hope anyone can help me.
Here is the code related to this form:
urls.py
urlpatterns = patterns(
'',
url(r'^admin_fichiers_phyto/$', phyto_views.AdminFichiersPhyto.as_view(), name='phyto-admin-fichiers-phyto'),
)
phyto_admin_fichiers.html
{% block forms %}
{% if user.is_staff%}
<fieldset>
<div>
<span>{{ form.other }}</span>
</div>
</fieldset>
<p>
<input id="submit" class="btn btn-primary" type="submit" value="Synchronisation Autre" name="autre"/>
<input id="submit" class="btn btn-primary" type="submit" value="Synchronisation Traitements généraux" name="trtm_gen"/>
</p>
{% endif %}
{% endblock %}
views.py
class AdminFichiersPhyto(TemplateView):
template_name = 'phyto/phyto_admin_fichiers.html'
form_class = forms.PhytoFileForm
current_url = 'phyto-files'
context_object_name = 'phyto_files'
def post(self, request, *args, **kwargs):
if request.POST.get('autre'):
return HttpResponse('<h1>autre</h1>')
if request.POST.get('trtm_gen'):
return HttpResponse('<h1>Traitement Generaux</h1>')
forms.py
class PhytoFileForm(forms.Form):
class Meta:
model = models.PhytoFile
fields = ['general_treatment', 'other']
def __init__(self, *args, **kwargs):
super(PhytoFileForm, self).__init__(*args, **kwargs)
models.py
class PhytoFile(models.Model):
general_treatment = models.FileField(upload_to='fichiers_phyto/', blank=True, null=True)
other = models.FileField(upload_to='fichiers_phyto/', blank=True, null=True)
Here is what my webpage is showing :
https://imgur.com/a/yH0be0K
I can't understand why the Field isn't displayed, I really hope somebody have the knowledge to help me with my problem ! ^_^
Have a nice day ! :D

You have several major issues here.
TemplateView doesn't know anything about form_class (or context_object_name, for that matter). And by defining post like that you've avoided actually doing anything with the uploaded data. You need to use a view that deals with forms; in your case, a CreateView would be ideal. Inside that view, that response code needs to go in form_valid.
Secondly, PhytoFileForm needs to inherit from forms.ModelForm; a standard Form class doesn't know anything about models and doesn't use a Meta class. However, since you're not customizing the form beyond the field selection, you can just rely on the automatic form created by CreateView.
Next, your template is broken; it doesn't have an HTML form element. Also, since your form class has two fields but you only display one on the template, the form will never be valid.
So, putting it together:
class AdminFichiersPhyto(CreateView):
template_name = 'phyto/phyto_admin_fichiers.html'
model = models.PhytoFile
def form_valid(self, form):
form.save()
if request.POST.get('autre'):
return HttpResponse('<h1>autre</h1>')
if request.POST.get('trtm_gen'):
return HttpResponse('<h1>Traitement Generaux</h1>')
{% block forms %}
{% if user.is_staff%}
<form method="post" action="" enctype="multipart/form-data">
<fieldset>
<div>
<span>{{ form.other }}</span>
</div>
<div>
<span>{{ form. general_treatment }}</span>
</div>
</fieldset>
<p>
<input id="submit" class="btn btn-primary" type="submit" value="Synchronisation Autre" name="autre"/>
<input id="submit" class="btn btn-primary" type="submit" value="Synchronisation Traitements généraux" name="trtm_gen"/>
</p>
</form>
{% endif %}
{% endblock %}

Related

Show django form in a designed page

How are you?
I m totally new in Django.I designed a page and I wanted to show a django form(edit or create) in a well designed HTML page. but i do not know how.
This is my owner method:
class OwnerUpdateView(LoginRequiredMixin, UpdateView):
"""
queryset to the requesting user.
"""
def get_queryset(self):
print('update get_queryset called')
""" Limit a User to only modifying their own data. """
qs = super(OwnerUpdateView, self).get_queryset()
return qs.filter(user=self.request.user)
class OwnerCreateView(LoginRequiredMixin, CreateView):
"""
Sub-class of the CreateView to automatically pass the Request to the Form
and add the owner to the saved object.
"""
# Saves the form instance, sets the current object for the view, and redirects to get_success_url().
def form_valid(self, form):
print('form_valid called')
object = form.save(commit=False)
object.user = self.request.user
object.save()
return super(OwnerCreateView, self).form_valid(form)
This is my views.py
class TaskUpdateView(OwnerUpdateView):
model = Task
fields = ["title", "text", "endDate"]
class TaskCreateView(OwnerCreateView):
model = Task
fields = ["title","text","status","endDate"]
This is my urls.py:
app_name='task'
urlpatterns = [
path('', views.TaskListView.as_view(), name='all'),
path('task/<int:pk>/', views.TaskDetailView.as_view(), name='detail'),
path('task/create', views.TaskCreateView.as_view(success_url=reverse_lazy('task:all')), name='task_create'),
path('task/update/<int:pk>', views.TaskUpdateView.as_view(success_url=reverse_lazy('task:all')),
name='task_update'),
path('task/delete/<int:pk>', views.TaskDeleteView.as_view(success_url=reverse_lazy('task:all')),
name='task_delete'),
path("accounts/login/", views.login, name='login'),
path("accounts/logout/", views.logout, name='logout'),
]
And this is the models.py:
class Task(models.Model):
title=models.CharField(max_length=250)
text=models.TextField()
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False)
status=models.ForeignKey('Status',on_delete=models.SET_NULL,null=True)
startDate=models.DateTimeField(auto_now_add=True)
endDate=models.DateField(null=True)
def __str__(self):
return self.title
class Status(models.Model):
name=models.CharField(max_length=250)
def __str__(self):
return self.name
And this is where these both function work:
{%extends 'base.html'%}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<table>{{ form.as_table }}</table>
<input type="submit" value="Submit">
{# <input type="submit" onclick="window.location='{% url 'project:all' %}' ; return false;" value="Cancel">#}
</form>
{% endblock %}
How can i separate each element of this form and put it in a better designed page?
Thanks
There are two ways:
Option 1:
Loop over the form fields and render them individually:
{% for field in form %}
<div class="form-group">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<span class="form-text">{{ field.help_text|safe }}</span>
{% endif %}
</div>
{% endfor %}
See docs for more.
Option 2:
You can manually create form inputs and give them the correct field name attribute. This gives you more control but also requires more work:
<div class="form-group"
<input
type="text"
name="title"
value="{{ form.title.value }}"
class="form-control {% if form.title.errors %}is-invalid{% endif %}"
>
{% if form.title.help_text%}
<span class="form-text">{{ form.title.help_text|safe }}</span>
{% endif %}
<div class="invalid-feedback">{{ form.title.errors }}</div>
</div>
<!-- now do the same for other fields -->

How do I modify existing fields in django that have a many to many relationship?

I have a model with a many to many field (participant field in my Activity model, see below) that I want to edit using createview. However, I can only choose from the already existing entries, not create new ones. It is possible to do that in the admin site so there is a solution but I can't figure it out. I have tried to modify form_valid in views but with no succes. Any ideas how to add or modify a field that has a many to many relation?
views.py:
#response=super(CreateTour,self).form_valid()
#self.object.project=self.form.cleaned_data['participant']
class CreateTour(CreateView):
form_class=CreateTourForm
template_name='artdb/createtour.html'
def get_context_data(self,**kwargs):
context=super(CreateTour,self).get_context_data(**kwargs)
context['formset']=CreateFormset()
return context
def form_valid(self,form):
self.object=form.save(commit=False)
for p in form.cleaned.data['participant']:
ap=Person()
ap.group=self.object
ap.person=ap
ap.save()
return super(self).form_valid(form)
models.py:
class Activity(models.Model):
activity_name=models.CharField(max_length=200,default='no name')
project=models.ForeignKey(Project,on_delete=models.CASCADE,default=1)
participant=models.ManyToManyField(Person)
min_stage_area=models.IntegerField(default='2')
light_requirements=models.CharField(max_length=200,default='no requirements')
sound_engineer=models.CharField(max_length=200,default='not needed')
comment=models.ManyToManyField(Comment)
def __str__(self):
return self.activity_name
class Meta:
ordering = ('activity_name',)
forms.py:
class CreateTourForm(ModelForm):
class Meta:
model=Activity
fields=('activity_name','project','participant')
widgets={'participant':CheckboxSelectMultiple,}
CreateFormset=modelformset_factory(
Activity,
fields=['activity_name','participant'],
extra=1,
widgets={
'date':DateInput(attrs={'type': 'date'}),
}
)
template:
{% extends "artdb/index.html" %}
{% block ct %}
<form method="post">{% csrf_token %}
<div class="input-group">
{% for fr in formset %}
{{fr}}
{% endfor %}
<a>
{{form}}
</a>
<div class="input-group-append">
<button class="btn btn-success add-form-row">+</button>
</div>
</div>
<hr></hr>
<div class="row spacer">
<input type="submit" value="save">
</div>
</form>
{% endblock ct %}

Django CreateView form not getting inserted

I'm fairly new to this, but what I'm trying to do is get my form to display (injected as part of a template) in another view. In developer tools I see the HTML for my included page (polls/_poll_form.html), but not the form. I would appreciate it if someone could point me in the right direction.
models.py
class Poll(models.Model):
poll_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, unique=True)
topic = models.ForeignKey(
Topic,
related_name = 'polls',
on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
last_updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse(
'polls:single',
kwargs={'pk':self.pk}
)
class Meta:
db_table = 'polls'
ordering = ['last_updated_at']
views.py
class CreatePoll(LoginRequiredMixin, generic.CreateView):
template_name = 'polls/_poll_form.html'
model = Poll
_poll_form.html (injected template)
<div class="container poll-form-header">
<p class="text-center">Get Started</p>
</div>
<form class="create-poll-form" action="{% url 'topics:single' pk=topic.topic_id %}" method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" class="btn btn-dark float-right">
</form>
topic_detail.html
{% extends "topics/topic_base.html" %}
{%block topics_content %}
<div class="col-md-12 list-group polls-list">
<div class="container new-poll-button">
<a class = "btn btn-dark float-right mt-2" data-toggle="collapse" href="#poll-form" role="button" aria-expanded="false">Create Poll</a>
</div>
<div class="collapse mt-2 new-poll-form" id="poll-form">
<div class="card card-body">
{% include "polls/_poll_form.html" %}
</div>
</div>
{% if topic.polls.count == 0 %}
<br>
<div class="container no-polls-message">
<p>There are no polls for this topic. Create the first!</p>
</div>
{% else %}
{% for poll in topic.polls.all %}
{% include "polls/_poll.html" %}
{% endfor %}
{% endif %}
</div>
{% endblock %}
This appears to be a fairly common confusion, but I don't really understand how it arises.
Just including a template in another one doesn't mean that a view which mentions that template is executed. Views render templates, templates don't call views. Views are only called by the user requesting a URL which is handled by that view. In your case, the URL is pointing to a completely different URL, and the one that creates the form is never called.
You need to include the form in the context of the view that your URL is actually calling. Either do this explicitly in the get_context_data method, or - if the form needs to appear on multiple pages - create a custom template tag that inserts a rendered template, including the form.
Make a forms.py in your app.
Write something like this:
from .models import Poll
class PollForm(forms.ModelForm):
class Meta:
model = Poll
fields = ('name', 'topic',)
And then import PollForm in views.py and pass it to template
from polls.forms import PollForm
class CreatePoll(LoginRequiredMixin, generic.CreateView):
template_name = 'polls/_poll_form.html'
model = Poll
form_class = PollForm

Refering to related object in ListView

I am trying to create some kind of 'panel control'. I need to manage my objects directly from ListView. I'll show you transparent (I believe) schema what I'm trying to solve.
Models
class Category(models.Model):
cat_name = models.CharField(max_length=120)
class Product(models.Model):
category = models.ForeignKey(Category)
prod_name = models.CharField(max_length=120)
Views
class CategoryListView(ListView):
model = Category
def get_context_data(self, *args, **kwargs):
context = super(CategoryListView, self).get_context_data(*args, **kwargs)
# two class based forms created for models
context['form_category'] = CategoryForm()
context['form_product'] = ProductForm()
return context
def post(self, request, *args, **kwargs):
form_category = CategoryForm(request.POST or None)
if form_category.is_valid():
new_category = form_category.save(commit=False)
new_category.save()
return redirect('category_list')
return Http404
templates
I use two modal window to open my forms which I added to my context views
<button>Modal btton opens product form</button>
{% for category in object_list %}
<div class="panel">
<div class="panel-heading">
<h3>{{ category.cat_name}}</h3>
<button>Modal btn opens product form</button>
</div>
<div class="panel-body">
<ul>
{% for product in category.product_set.all %}
<li>
<p>{{ product.prod_name }}<p>
<span>Delete</span>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
<form action="." method="POST">{%csrf_token%}
{{ form_category.as_p }}
<input type="submit" value="add category">
</form>
<form action="." method="POST">{%csrf_token%}
{{ form_product.as_p }}
<input type="submit" value="add product">
</form>
For first form (CategoryForm) it's working fine because whole page refers to that (model = Category). Now is the question - how to create second object on that page. I can do this in DetailView using urls and parameter like slug, id or pk and after passing it as value but I want to do this from that page without moving to detail page.
Secondly I was thinking how to delete products from list but there is the same problem how to let django know that witch object I would like to remove.
Thanks.

Why is Django widgets for TimeInput not showing

I'm trying to create a TimeInput field in a form and noticed that the widget isn't showing correctly. But when I check the localhost:8000/admin, I see the widget showing up correctly.
My code is as follows. For models.py,
class TimeLimit(models.Model):
before = models.TimeField(blank=True, default=time(7, 0)) # 7AM
after = models.TimeField(blank=True, default=time(23, 0)) # 11PM
For views.py,
class UpdateTimeLimitView(LoginRequiredMixin, FormView):
model = TimeLimit
template_name = 'accounts/update_time_limit.html'
form_class = UpdateTimeLimitForm
def get_success_url(self):
return reverse_lazy('accounts:user_profile') + '?username=' + self.request.GET['username']
def get_context_data(self, **kwargs):
data = super(UpdateTimeLimitView, self).get_context_data(**kwargs)
data['username'] = self.request.GET['username']
return data
For forms.py,
class UpdateTimeLimitForm(forms.Form):
time_error = {'required': 'This field is required.',
'invalid': 'Please enter valid Hour:Minute values.'}
before = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
after = forms.TimeField(widget=TimeInput(format='%H:%M'))
class Meta:
model = TimeLimit
Finally, the relevant part for fields in update_time_limit.html,
<div class="container">
<form method="post">
{% csrf_token %}
<p>
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}({{ field.help_text }}):</label>
<br />
{{ field }}<br /><br /> and
{% endfor %}
</p>
<input class="btn btn-primary done-btn" type="submit" value="Update Time Limit">
</form>
</div>
Is there anything that I'm missing or doing wrong? Thank you.
The Django admin uses AdminTimeWidget to display time fields, not the TimeInput widget that you are using in your code.
There isn't a documented way to reuse the AdminTimeWidget outside of the Django admin. Getting it to work is very hacky (see the answer on this question, which is probably out of date), so it's probably better to use a different widget.
convert datetime.time(7, 0) to string work for me.
data['before'] = data['before'].strftime('%H:%M:%S')