I am working on extending the webapp we're left off with after completing the official Django Tutorial.
One of the functionalities I am looking to add is the ability for users to add polls themselves.
I am struggling with getting the page to process the data and then redirect to the index page ('/polls').
When I submit a new poll as a logged in user, I am returned to my index page, which is supposed to show most recently published polls, or in the event of no polls, the message "No polls are available."
For some reason, I always see "No polls are available", but once I click to the index page via a link on the site, it displays all of my polls, including my most recently created one, data intact!
Any thoughts here? I think I have included the relevant info below but happy to supply more. Thanks in advance for any help/advice.
views.py
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Poll.objects.order_by('-pub_date')[:15]
#login_required
def add_poll(request):
ChoiceFormSet = formset_factory(ChoiceForm, extra=3, min_num=2, validate_min=2)
if request.method == 'POST':
form = PollForm(request.POST)
formset = ChoiceFormSet(request.POST)
if all([form.is_valid(), formset.is_valid()]):
poll = form.save()
for inline_form in formset:
if inline_form.cleaned_data:
choice = inline_form.save(commit=False)
choice.question = poll
choice.save()
return render(request, 'polls/index.html', {})
else:
form = PollForm()
formset = ChoiceFormSet()
return render(request, 'polls/add_poll.html', {'form': form, 'formset': formset})
add_poll.html
{% extends 'polls/base.html' %}
{% block title %}Add Poll{% endblock %}
{% block body_block %}
<form role="form" id="poll_form" method="post" action="{% url 'polls:add_poll' %}">
<h2 class="form-signin-heading">Add a Poll</h2>
{% csrf_token %}
<table>
{{ form }}
{{ formset }}
</table>
<br/>
<button class="btn btn-primary" type="submit" name="submit">Create Poll</button>
</form>
{% endblock %}
index.html
{% extends 'polls/base.html' %}
{% block body_block %}
{% if latest_question_list %}
<ul>
{% for poll in latest_question_list %}
<li>{{ poll.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
{% endblock %}
urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^add_poll/$', views.add_poll, name='add_poll'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^profile_page/$', views.ProfileView.as_view(), name='profile_page'),
url(r'^edit_profile/$', views.edit_profile, name='edit_profile'),
)
When you save your form you are not redirecting.
Your are returning 'polls/index.html' with empty polls data, that's why you always get "No polls are available". But this is very incorrect, you must follow the Post/Redirect/Get (PRG) pattern, so instead of:
return render(request, 'polls/index.html', {})
do this:
return HttpResponseRedirect(reverse('polls:index'))
You don't do any redirect right now, you are just rendering your index template with an empty context (that's why you don't see anything). To redirect, you need to use HttpResponseRedirect when your form is valid.
So, please change line:
return render(request, 'polls/index.html', {})
(just over the else) to
return HttpResponseRedirect(reverse('index'))
I finally figured it out. I had to change my html form to this:
<form method="POST" action="{% url 'new_beam:beam_diagram' beam_id=1 %}" enctype="multipart/form-data">
Related
Note: there's a similar question to mine, there're a few differences in our code, and I tried the solutions to his question, didn't work for me.
I'm working on a blog using Django, I'm trying to add an edit_post feature but when I go to localhost:8000 it shows NoReverseMatch, the problem is related to the post's id, I'm trying to create a home page that shows the post's title and it's content.
here'e my code:
models.py
from django.db import models
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length=20)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
urls.py
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns = [
# Home page
path('', views.index, name='index'),
# page for adding a new post
path('new_post/', views.new_post, name='new_post'),
# page for editing a post
path('edit_post/<int:post_id>/', views.edit_post, name='edit_post'),
]
index.html
{% block content %}
<p>My personal Blog</p>
Add a new post
<ul>
{% for p in posts %}
# the problem is probably here
<li>{{ p.title }}</li>
<li>{{ p.text }}</li>
<p>
Edit post
</p>
{% endfor %}
</ul>
{% endblock content %}
edit_post.html
{% block content %}
<p>Edit post:</p>
<form action="{% url 'blogs:edit_post' post.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submet">Save changes</button>
</form>
{% endblock content %}
new_post.html
{% block content %}
<P>Add a new post:</P>
<form action="{% url 'blogs:new_post' %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submet">Add post</button>
</form>
{% endblock content %}
views.py
from django.shortcuts import render, redirect
from .models import BlogPost
from .forms import BlogPostForm
# Create your views here.
def index(request):
posts = BlogPost.objects.order_by('date_added')
context = {'posts': posts}
return render(request, 'blogs/index.html', context)
def new_post(request):
"""Add a new post."""
if request.method != 'POST':
form = BlogPostForm()
else:
form = BlogPostForm(data=request.POST)
if form.is_valid():
new_p = form.save()
return redirect('blogs:index')
context = {'form': form}
return render(request, 'blogs/new_post.html', context)
def edit_post(request, post_id):
post = BlogPost.objects.get(id=post_id)
if request.method != 'POST':
form = BlogPostForm(instance=post)
else:
form = BlogPostForm(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:index', post_id=post.id)
context = {'post': post, 'index': index, 'form': form}
return render(request, 'blogs/edit_post.html', context)
forms.py
from django import forms
from .models import BlogPost
class BlogPostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['title', 'text']
labels = {'text': ''}
widgets = {'text': forms.Textarea(attrs={'cols':80})}
The problem is with the arguments of the url you are creating on index.html, you are looping posts as p in html so try to change:
Edit post
With
Edit post
I don't know why the variables from the register form doesn't store in django admin page! I'm confused. Thank you for read this, I really appreciate this. :)
//htmlsnippet.html
{% block content %}
<h1>Sign up</h1>
<form class ="site-form" action="register/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Signup">
</form>
{% endblock %}
//home.html
{% extends "register/header.html" %}
{% block content %}
{% include "register/includes/htmlsnippet.html" %}
{% endblock %}
//views.py
from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm
# Create your views here.
def index(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect("/personal")
else:
form = UserCreationForm()
return render(request, 'register/home.html',{'form':form})
well yeah because the form was in the website. the problem is that when I put in values in these input, it doesn't save in the admin. this is my url for register (
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name='index')#,url(r'^register', views.post, name='post')
]
")
this is my url pattern .
urlpatterns = [
url(r'^register/$', include('register.urls'))
For some reason, my forms.py doesn't view any of the fields, instead, it only shows the 'Add' button and I don't know what to do anymore. I'd really appreciate if someone who knows what they're doing could tell me what I did, or didn't do.
Please note that I'm new to Django, thank you.
Here's my views.py:
from django.shortcuts import render
from django.utils import timezone
from .models import Measurement
from .forms import MeasurementForm
from django.views import generic
class IndexView(generic.ListView):
model = Measurement
context_object_name = 'measurement_list'
template_name = 'index.html'
queryset = Measurement.objects.all()
def new_measurement(request):
if request.method == "POST":
form = MeasurementForm(request.POST)
if form.is_valid():
measurement = form.save(commit=False)
measurement.measurement_date = timezone.now()
measurement.save()
else:
form = MeasurementForm()
return render(request, 'index.html', {'form': form})
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
]
forms.py:
from django import forms
from .models import Measurement
class MeasurementForm(forms.ModelForm):
class Meta:
model = Measurement
fields = ('measurement_value', 'measurement_unit')
index.html:
{% extends "base.html" %}
{% block content %}
<h1>Climate Measurement Tool</h1>
<h2>Add a new measurement</h2>
<form method="POST" class="post-form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save">Add</button>
</form>
<h2>Measurements</h2>
{% if measurement_list %}
<ul>
{% for measurement in measurement_list %}
<li>
<p>{{ measurement }}</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>No measurements yet</p>
{% endif %}
{% endblock %}
The new_measurement() view correctly initializes a form instance and passes it to the template.
Unfortunately, that view is never called.
urls.py defines only one url, handled by IndexView.as_view(), which does not pass a form instance to the template.
I need one help. I need to implement the forget password functionality using Django. I am using the Django signup and login page. My code is below:
login.html:
{% extends 'base.html' %}
{% block content %}
<h2>Log in</h2>
{% if form.errors %}
<p style="color: red">Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}<br>
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% if field.help_text %}
<p><small style="color: grey">{{ field.help_text }}</small></p>
{% endif %}
</p>
{% endfor %}
<button type="submit">Log in</button>
New to My Site? Sign up
</form>
{% endblock %}
views.py:
class Signup(View):
""" this class is used for user signup """
def get(self, request):
""" this function used to get the sign up form """
form = UserCreationForm()
return render(request, 'plant/signup.html', {'form': form})
def post(self, request):
""" this function used for post the sign up data """
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
class AuthLogin(View):
""" Its for login """
def get(self, request):
""" this function used to get the login form """
form = AuthenticationForm()
return render(request, 'plant/login.html', {'form': form})
def post(self, request):
""" this function used for post the login data """
form = AuthenticationForm(None, request.POST or None)
if form.is_valid():
login(request, form.get_user())
return redirect('/')
urls.py:
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="plant/index.html")),
url(r'^logout/$', auth_views.logout,
{'next_page': 'login'}, name='logout'),
url(r'^login/$', core_views.AuthLogin.as_view(), name='login'),
url(r'^signup/$', core_views.Signup.as_view(), name='signup'),
]
In login page I should have the forget password link. When user will click on it, the reset password page will open and another one condition is after trying 3 wrong attempt the forget password button will be invisible for 1 hour.
Do not create this functionality yourself but use the built-in Django auth views. https://docs.djangoproject.com/en/1.11/topics/auth/default/#built-in-auth-views
The only thing you need to do is add the contrib auth urls to your project:
urlpatterns = [
url('^', include('django.contrib.auth.urls')),
]
This gives you all views like login, logout, password reset etc.
If you want to customise the templates, copy the templates form /path/to/site-packages/django/contrib/admin/templates/registration/ to your app project/app/templates/registration/ and make any customisations
there.
If your app is listed before 'django.contrib.auth' in INSTALLED_APPS (it should always be like that) your custom templates will be picked first.
I always get this error:
Reverse for 'user_home' with arguments '()' and keyword arguments '{u'pk': None}' not found. 1 pattern(s) tried: ['ibs/(?P[0-9]+)/home/$']
in my template I can display my user.pk (it is equal to 1) but when i use it and pass it in my views i got the error above (pk: none). I have read some threads about this. Some are because of the url patterns but I changed and check my urls but nothing happened. Still gets the error.
this is my url:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^ibs/$', views.index, name='index'),
url(r'^ibs/register/$', views.user_add, name='user_add'),
url(r'^ibs/login/$', views.user_login, name='user_login'),
url(r'^ibs/(?P<pk>[0-9]+)/home/$', views.user_home, name='user_home'),
url(r'^ibs/(?P<user_pk>[0-9]+)/logout/$', views.user_logout, name='user_logout'),
url(r'^ibs/(?P<pk_item_add>[0-9]+)/additem/$', views.item_add, name='item_add'),
url(r'^ibs/(?P<pk_item>[0-9]+)/viewitem/(?P<pk_user>[0-9]+)/$', views.item_detail, name='item_detail'),
]
my template:
{% block home %}
<li class="active">Home</li>
{% endblock home %}
{% block register %}
{% endblock register %}
{% block login %}
{% endblock login %}
{% block logout %}
<li>
<a method="post" href="{% url "user_logout" user_pk=user.pk %}"><span class="glyphicon glyphicon-log-out"></span> Logout
</a>
</li>
{% endblock logout %}
{% block post %}
<li>
<a method="post" href="{% url "item_add" pk_item_add=user.pk %}"> {{user.pk}}
</a>
</li>
{% endblock post %}
def user_login(request):
if request.method == "POST":
#username is unique, get User where username is equal to requested username
username = User.objects.get(username = request.POST['username'])
if username.password == request.POST['password']:
request.session['username'] = username.id
return redirect('system.views.user_home', pk=username.id)
else:
return render(request, 'system/user_login.html')
views.py
def user_home(request, pk):
user = get_object_or_404(User, pk=pk)
try:
if request.session['username'] == user.pk:
items = Item.objects.all()
types = Type.objects.all()
return render(request, 'system/user_home.html', {'user':user, 'types':types,
'items': items})
else:
return redirect('system.views.user_login')
except KeyError:
return redirect('system.views.user_login')
def user_logout(request, user_pk):
user = get_object_or_404(User, pk=user_pk)
try:
del request.session['username']
return redirect('system.views.user_login')
except KeyError:
pass
return redirect('system.views.user_login')
Where did I go wrong? I passed all the necessary data from views to templates.
When a user is not logged in the user is likely the models.AnonymousUser which does not have a primary key.
https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#anonymous-users
If the user is not logged in then you probably don't want them to see that template at all. Redirect them to the login page in the view or check that the user is not anonymous in the template.