I am trying to figure out how to insert a default item which is "Item Code" into my 'List' database instantaneously after clicking the add button. I only managed to do is insert the string/char that I have entered in the text box. Though, when I click the 'add' button without typing in anything inside the text box, I get this error:
ValueError at /
The view home.views.index didn't return an HttpResponse object. It returned None instead.
Here are my codes so far:
models.py
from django.db import models
from django.utils import timezone
class List(models.Model):
item = models.CharField(max_length=200)
date = models.DateTimeField(default = timezone.now)
def __str__(self):
return self.item
views.py
from django.shortcuts import render
from .models import List
from .forms import ListForm
from django.contrib import messages
def index(request):
context = {
'items': List.objects.all()
}
if request.method == 'POST':
form = ListForm(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, ('Item has been added to the list.'))
return render(request, 'home/index.html', context)
else:
return render(request, 'home/index.html', context)
forms.py
from django import forms
from .models import List
class ListForm(forms.ModelForm):
class Meta:
model = List
fields = [
'item',
'date'
]
index.html
<h1>To do list</h1>
{% if messages %}
{% for message in messages %}
<p> {{ message }} </p>
{% endfor %}
{% endif %}
<ul>
{% if items %}
{% for item in items %}
<li>{{ item.item }}</li>
<li>{{ item.date }}</li>
<br>
{% endfor %}
{% endif %}
</ul>
<form method="POST">{% csrf_token %}
<input type="text" name="item">
<input type="submit" value="Add">
</form>
Remove the else statement and unindent the return. You always want to return that rendered template, except when you redirect after a successful post.
Related
Good afternoon all,
One of my form does not seem to save when submitted. I cannot see why, in particular as I have a similar form working just fine using the same code.
For some reason it work just fine using the admin panel.
My assumption is that I am missing something that tells the form it needs to be saved. But cannot find what.
Any ideas?
Models
RATING=(
(1,'1'),
(2,'2'),
(3,'3'),
(4,'4'),
(5,'5'),
)
class ProductReview(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
product=models.ForeignKey(Product,related_name="comments", on_delete=models.CASCADE)
review_text=models.TextField(max_length=250)
review_rating=models.IntegerField(choices=RATING,max_length=150, default=0)
date_added = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
Views
def add_review(request, product_id):
product = Product.objects.get(pk=product_id)
form = ReviewAdd(request.POST or None, instance=product) #instance=product (populate field with existing information)
if form.is_valid():
form.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
URL
from django.urls import path
from . import views
urlpatterns = [
...
path('product/add_review/<product_id>', views.add_review,name="add_review"),
]
Forms
class ReviewAdd(forms.ModelForm):
class Meta:
model = ProductReview
fields = ('review_text', 'review_rating')
labels ={
'review_text': '',
'review_rating': '',
}
widgets = {
'review_text': forms.TextInput(attrs={'class':'form-control', 'placeholder':'Enter Review'}),
}
Admin
from django.contrib import admin
from .models import Venue, User, Product, ProductReview
from django.urls import path
admin.site.register(User)
admin.site.register(ProductReview)
class ProductReview(admin.ModelAdmin):
list_display=['user','product','review_text','get_review_rating']
HTML Page
{% extends 'main/base.html' %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock %}
{% block content %}
<center>
<h1>Add ReviewTo Database</h1>
<br/><br/>
{% if submitted %}
Success!
{% else %}
<form action="" method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="Submit" value="Submit" class="btn btn-secondary">
</form>
{% endif %}
</center>
{% endblock %}
I detect 2 fixes
On your url, the parameter product_id might need the type of data it will going to receive
path('product/add_review/<int:product_id>', views.add_review,name="add_review"),
And in your view, you are sending an instance, and no data for a new rating.
In your view:
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
#login_required
def add_review(request, product_id):
product = get_object_or_404(Product, pk=product_id)
form = ReviewAdd(request.POST or None)
if form.is_valid():
new_rating = form.save(commit=False)
new_rating.product = product
new_rating.user = request.user
new_rating.save()
return redirect('product')
return render(request, 'main/add_review.html',{'form':form})
I'm using this app to register the user of my website https://github.com/egorsmkv/simple-django-login-and-register. The problem is that no metter what I do my form are not visible (they were when I did not use this registration app and the code worked just fine). This is my code:
model
class UserBio(models.Model):
name = models.CharField(max_length=120)
age = models.CharField(max_length=2)
phone = models.CharField(max_length=10)
height = models.CharField(max_length=3)
weight = models.CharField(max_length=3)
form
class UserBio(forms.ModelForm):
class Meta:
model = UserBio
fields = (name', 'age', 'phone', 'height', 'weight')
views
def add_bio(request):
submitted = False
if request.method == "POST":
info_form = UserBio(request.POST)
if info_form.is_valid():
info_form.save()
return HttpResponseRedirect('add_information?submitted=True')
else:
info_form = UserBio()
if 'submitted' in request.GET:
submitted = True
return render(request, 'accounts/profile/add_information.html', {'form': info_form, 'submitted':submitted})
urls
urlpatterns = [
path('add/information', views.add_information, name='add_information'),
]
html
{% extends 'layouts/default/base.html' %}
{% block title %} Add info {% endblock %}
{% load i18n %}
{% block content %}
<h4>{% trans 'Add Info' %}</h4>
{% if submitted %}
Sumitted correctly
{% else %}
<form method="post">
{% csrf_token %}
{{ info_form.as_p }}
</form>
</div>
<br/>
</body>
{% endif %}
{% endblock %}
Any help would be very apprecieted!
because in your views def add_bio change your url acc to your function views
path('add/information', views.add_bio, name='add_information'),
and in you template
{{ form.as_p }}
You passed the info_form variable to the template with variable name form. Indeed:
# name of the variable for the template ↓
return render(request, 'accounts/profile/add_information.html', {'form': info_form, 'submitted':submitted})
This thus means that you render this with:
{{ form.as_p }}
You should also trigger the correct view:
urlpatterns = [
path('add/information/', views.add_bio, name='add_information'),
]
The path does not point to the template: a path points to a view, and the view can (this is not required) render zero, one or multiple templates to create a HTTP response.
I am relatively new to django and i'm trying to implement some modelforms.
My page consists of two views, a Politics section and a Sports section, each one with the same form for making comments (my comment model is named Comentario). It has a field for the content and a field for the section the comment belongs to. Both views are basically the same so I'm going to showcase just the politics one:
from django.contrib import messages
from django.shortcuts import render
from django.views.generic import CreateView
from usuarios.models import Usuario
from .forms import CrearComentario
from .models import Comentario
usuarios = Usuario.objects.all()
comentarios = Comentario.objects.all()
pag = ''
def politics(request):
if request.user.is_authenticated:
if request.method == 'POST':
form = CrearComentario(request.POST, instance=request.user)
if form.is_valid():
messages.success(request, 'Publicado!')
pag = 'politics'
form.save()
form = CrearComentario()
else:
form = CrearComentario(request.POST,instance=request.user)
else:
messages.warning(request, 'Comentario no válido')
form = CrearComentario(request.POST)
return render(request, 'main/politics.html', {'usuarios': usuarios,
'comentarios': comentarios,
'form': form})
In case you're wondering, 'pag' is a control variable that is checked by my signals.py file to update the 'pagina' field
I had trouble with the submit buttons in my custom modelsforms, the form displays correctly, and when I write something in the form and submit it, it displays a success message but the comment doesn't appear in the comment section and it doesn't appear in the django shell either.
politics.html
{% extends 'main/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
<!-- Here would be the content-->
{% block comentarios %}
<h3>Comentarios</h3>
<ul class="a">
{% for comment in comentarios %}
{% if comment.pagina == 'politics' %}
<li>
<span>{{ comment.contenido }}</span>
<br>
<small>{{ comment.usuario }} , {{ comment.fecha }}</small>
<hr>
<br>
</li>
{% endif %}
{% endfor %}
</ul>
{% if user.is_authenticated %}
<form method="POST" enctype="multipart/form-data" action="http://localhost:8000/main/politics/">
{% csrf_token %}
<fieldset class="form-group">
<legend>Dejanos tu opinion</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">OK</button>
</div>
</form>
{% else %}
<legend>Inicia sesión para poner comentarios</legend>
{% endif %}
{% endblock %}
My forms.py looks like this:
from django import forms
from .models import Comentario
class CrearComentario(forms.ModelForm):
contenido = forms.CharField(max_length = 250, required=False, widget=forms.Textarea)
pagina = forms.CharField(max_length = 250, required=False, widget=forms.HiddenInput())
class Meta:
model = Comentario
fields = ['contenido', 'pagina']
The field that determines to which section the comment belongs to ('pagina') is hidden because it's meant to be set by my signals.py file:
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Comentario
from .views import pag
from .forms import CrearComentario
#receiver(pre_save, sender=Comentario)
def fijar_pagina(sender, instance, **kwargs)
if pag:
instance.pagina = pag
pag = ''
instance.save(update_fields['pagina'])
I'm not getting any error message, and everything behaves like it should except for the fact that comments aren't being saved
I tried too a commit==False save instead of the signals but it was just as ineffective:
def politics(request):
if request.user.is_authenticated:
if request.method == 'POST':
form = CrearComentario(request.POST, instance=request.user)
if form.is_valid():
messages.success(request, 'Publicado!')
pag = 'politics'
comentario = form.save(commit=False)
comentario.pagina = 'sonsol'
comentario.save()
form = CrearComentario()
else:
form = CrearComentario(request.POST,instance=request.user)
else:
messages.warning(request, 'Comentario no válido)
form = CrearComentario(request.POST)
return render(request, 'main/politics.html', {'usuarios': usuarios,
'comentarios': comentarios,
'form': form})
usuarios and comentarios have both been defined at the module (file) level. As such they will not update for the lifetime of the process.
You should move both of these into the view body so that the query is run on every request
usuarios = Usuario.objects.all()
comentarios = Comentario.objects.all()
return render(request, 'main/politics.html', {'usuarios': usuarios,
'comentarios': comentarios,
'form': form})
I'm new to django and I'm having a lot of trouble with forms.
I'm making a calculation-based tool and I need to be able to have an arbitrary number of inputs.
As a really basic example, let's say I want to make a calculator that will sum and subtract any number of inputs. Each number to be added or subtracted is in its own number field. Both the list of "adding" fields and the list of "subtracting" fields has its own "add another field" button.
For starters, here's something that adds two inputs (since I can't figure out how to implement even 1 "add another field button" or understand the answer to it).
views.py
from __future__ import unicode_literals
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from .forms import AddForm
def _from_str(s):
try:
s = int(s)
except ValueError:
try:
s = float(s)
except ValueError:
pass
return s
#csrf_exempt
def web_adder(request):
if request.method == 'POST':
form = AddForm(request.POST)
# form = MyForm(request.POST, extra=request.POST.get('extra_field_count'))
if form.is_valid():
return web_adder_out(request, _from_str(form.cleaned_data['addend0']), _from_str(form.cleaned_data['addend1']))
else:
form = AddForm()
# form = MyForm()
return render(request, 'addercontent.html', {'form': form})
def web_adder_out(request, a, b):
return render(request, 'addercontentout.html', {'content':[a + b]})
forms.py
from django import forms
class AddForm(forms.Form):
addend0 = forms.CharField(label='first addend', max_length=100)
addend1 = forms.CharField(label='second addend', max_length=100)
addercontent.html
{% block content %}
<p>This is a web adder</p>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-default">Enter</button>
</form>
{% endblock %}
addercontentout.html
{% block content %}
{% for c in content%}
Result: {{c}}
<br>
Return
{% endfor %}
{% endblock %}
Don't use Django for the field generation. I would do all of it via HTML. Run your setup that you currently have, and you should be able to look at the page source to see how the inputs are structured. Then you can manually write the form in HTML, with JavaScript adding fields in as needed.
Something like this? (not tested, I haven't implement add button)
forms.py
class CalcForm(forms.Form)
first = forms.IntegerField()
second = forms.IntegerField()
def add(self):
first = self.cleaned_data['first']
second = self.cleaned_data['second']
return first + second
views.py
def index(request):
if request.method == "POST":
form = CalcForm(request.POST)
if form.is_valid():
result = form.add()
return render(request, 'your_result_template.html', {'result': result})
else:
form = CalcForm()
return render(request, 'your_template.html', {'form': form})
your_template.html
{% block content %}
<p>This is a web adder</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-default">Enter</button>
</form>
{% endblock %}
your_result_template.html
{% block content %}
<p>Sum:</p>
<h2>{{ result }}</h2>
{% endblock %}
Edit: For field generation you may need javascript.
I don't know why you want to use django for this kind of app.
I'm following a tutorial on effectivedjango.com, and this is the code they have:
views.py:
class CreateContactView(CreateView):
model = Contact
template_name = 'edit_contact.html'
fields = '__all__' #this is needed for error msg Using ModelFormMixin (base class of CreateContactView) without the 'fields' attribute is prohibited.
def get_success_url(self):
return reverse('contacts-list')
def get_context_data(self, **kwargs):
context = super(CreateContactView, self).get_context_data(**kwargs)
context['action'] = reverse('contacts-new')
return context
class UpdateContactView(UpdateView):
model = Contact
template_name = 'edit_contact.html'
fields = '__all__'
def get_success_url(self):
return reverse('contacts-list')
def get_context_data(self, **kwargs):
context = super(UpdateContactView, self).get_context_data(**kwargs)
context['action'] = reverse('contacts-edit', kwargs={'pk' : self.get_object().id})
return context
urls.py:
url(r'^$', contacts.views.ListContactView.as_view(),
name='contacts-list',),
url(r'^new$', contacts.views.CreateContactView.as_view(),
name='contacts-new',),
url(r'^edit/(?P<pk>\d+)/$', contacts.views.UpdateContactView.as_view(),
name='contacts-edit',),
contact_list.html:
{% block content %}
<h1>Contacts</h1>
<ul>
{% for contact in object_list %}
<li class="contact">
{{ contact }}
(edit)
</li>
{% endfor %}
</ul>
Add contact
{% endblock %}
edit_contact.html:
{% block content %}
{% if contact.id %}
<h1>Edit Contact</h1>
{% else %}
<h1>Add Contact</h1>
{% endif %}
<form action="{{ action }}" method="POST">
{% csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input id="save_contact" type="submit" value="Save" />
</form>
Back to list
{% if contact.id %}
Delete
{% endif %}
{% endblock %}
Why does the line context['action'] = reverse('contacts-edit', kwargs={'pk' : self.get_object().id}) in views.py look like its calling itself?
What I mean is, this action is called when the submit button is pressed in the contact-edit template, correct? So it starts there, and it is reverse-calling contact-edit which is itself, right?
What am I not seeing here?
Thank you for all your help.
Yes, the line context['action'] = reverse('contacts-edit', kwargs={'pk' : self.get_object().id}) in views.py is calling itself. This line generates the proper url for contacts-edit view.
This is done so that POST requests come to the same view i.e. UpdateContactView which is an UpdateView. There, proper handling will be done i.e. form validation will occur with the sent data. If the form is valid, object will be updated. Otherwise, the form will be displayed again with errors.
Django docs on UpdateView:
A view that displays a form for editing an existing object,
redisplaying the form with validation errors (if there are any) and
saving changes to the object.