Problems in finding urls from the application template in Django - python

Here is the full map of my Poll application:
My Project/
Poll_app/
__init__.py
settings.py
urls.py
wsgi.py
polls/
migrations/
__init__.py
0001_initial.py
templates/
polls/
detail.html
index.html
results.html
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
db.sqlite3
manage.py
Here, are the codes inside my files:
Poll_app/settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = 't#o_s8#^88go6lx9haux#b^p%&g63r)03ai!9cw7dm%h#2mjy^'
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['localhost']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'Poll_app.urls'
WSGI_APPLICATION = 'Poll_app.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
Poll_app/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin', include(admin.site.urls)),
)
polls/admin.py
from django.contrib import admin
from polls.models import Poll
admin.site.register(Poll)
polls/models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$',views.index,name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
polls/views.py
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from polls.models import Choice, Poll
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
def detail(request, poll_id):
try:
poll = Poll.objects.get(pk=poll_id)
except Poll.DoesNotExist:
raise Http404
return render(request, 'polls/detail.html', {'poll':poll})
def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/results.html', {'poll': poll})
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render(request, 'polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
My templates:
polls/templates/polls/detail.html:
<<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
polls/templates/polls/index.html:
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href="/polls/{{ poll.id }}/"{{ poll.question }}></a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
polls/templates/polls/results.html:
<h1>{{ poll.question }}</h1>
<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
Vote again?
I have run my app like following:
python manage.py runserver
When i visit (from localhost) ../polls/results, the page shows the message The requested URL /polls/results was not found on this server.
I am having same result for ..../polls/votes also.
N.B.
While i access ..../polls/ i see a 'dot' only.
I am stuck here from yesterday. I have tried to rebuild the app for 2 times. And yet, still getting the same result again and again. May be, there are spelling errors or problems in setting the Poll_app/settings.py. It would be really helpful for me, if you help me to figure out the problem.

I think the problem is in polls/urls.py, your regexp pattern only catches those urls of form "../polls/some_number/results" but you are trying "../polls/results" which will not be catched. In the regexp try replacing + with * and then try to access the url like this ^(?P<poll_id>\d*)/results/$.
Hope this helps.

Check your urls settings.
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote')
so you should visit localhost:port/polls/poll_id/results

Related

Why this form doesn't change the language of the website?

This is my language-switching form.
{% get_current_language as lang_code %}
{% get_available_languages as languages %}
{% get_language_info_list for languages as langs %}
<form action="{% url 'set_language' %}" method="POST">
{% csrf_token %}
<select name="language" title="{% translate 'Language' %}">
{% for lang in langs %}
<option value="{{ lang.code }}"{% if lang.code == lang_code %} selected{% endif %}>
{{ lang.name_local }} ({{ lang.name_translated }})
</option>
{% endfor %}
</select>
<input type="submit" value="OK" />
</form>
I include this in the footer of my base template.
Then, here is my courses/urls.py (app urls).
from django.contrib import admin
from django.urls import path
from django.utils.translation import gettext_lazy as _
from . import views
admin.site.site_header = _("FilFak administration")
admin.site.site_title = _("FilFak admin")
admin.site.index_title = _("Manage FilFak")
urlpatterns=[
path("courses/", views.CourseList.as_view(), name="course_list"),
path("professors/", views.ProfessorList.as_view(), name="professor_list"),
path("exams/", views.ExamList.as_view(), name="exam_list"),
path("courses/<slug:slug>", views.CourseDetails.as_view(), name="course_details"),
path("professors/<slug:slug>", views.ProfessorDetails.as_view(), name="professor_details"),
path("exams/<slug:slug>", views.ExamDetails.as_view(), name="exam_details"),
path("", views.Index.as_view(), name="index"),
]
filfak/urls.py (project urls.py)
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.conf import settings
from django.utils.translation import gettext_lazy as _
urlpatterns = [
path("admin/", admin.site.urls),
path("lang/", include("django.conf.urls.i18n")),
]
urlpatterns += i18n_patterns(
path("", include("courses.urls")),
prefix_default_language=False
)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here's the list of middlewares from settings.py.
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
When I click OK on the language-switching form, it actually changes the language only on the homepage of the default language. On any other URL it just refreshes the page, without changing the language.
Am I doing something wrong?
Check the flag value of USE_I18N, look like its set to False and thats why the language are not setting properly.
Change it to :-
USE_I18N = True
As per Django document :-
If you don’t use internationalization, you should take the two seconds to set USE_I18N = False in your settings file.

Django: NoReverseMatch not a valid view function or pattern name

I have a django project with a structure like this:
my_project
|_ UserSignUp
|_ urls.py
|_ views.py
|_ Mainpage
|_ urls.py
|_ views.py
|_ my_project
|_ urls.py
|_ settings.py
My problem is as follows:
I can access mysite.com/index and mysite.com/login just fine. But if I try to open mysite.com/signup I encounter a 500 Internal Server Error, more precisely an Error is thrown:
NoReverseMatch at /signup/
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Of course I already googled the error but did not encounter anything particularly helpful. The descriptions I found explained errors, in which an url of an app had been tried to access, then failing because the namespace of the app was not provided in the url tag. In my case I am inside an app and want to access an url of the project root. As far as I know it should be automatically resolved and even if not, I don't know how to tell django to please look into the root urls. Is django not checking the root urls really cause of the problem or is there another thing I set up wrong? How can I fix this?
My root urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', include('Mainpage.urls')),
path('index/', include('Mainpage.urls')),
path('mainpage/', include('Mainpage.urls')),
path('home/', include('Mainpage.urls')),
path('login/', auth_views.LoginView.as_view(template_name='login/login.html', redirect_field_name='index')),
path('signup/', include('UserSignUp.urls')),
path('logout', auth_views.LogoutView.as_view(template_name='logout/logout.html', next_page='index')),
path('admin/', admin.site.urls),
]
My UserSignUp urls.py:
from django.urls import path
from django.conf.urls import include
from UserSignUp import views
urlpatterns = [
path(r'', views.signup, name='signup'),
path(r'account_activation_sent/', views.account_activation_sent, name='account_activation_sent'),
path(r'activate/', views.activate, name='activate')
]
My UserSignUp views.py:
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import render, redirect
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from UserSignUp.forms import SignUpForm
from UserSignUp.tokens import account_activation_token
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
subject = 'Activate Your MySite Account'
message = render_to_string('account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
user.email_user(subject, message)
return redirect('signup/account_activation_sent')
else:
form = SignUpForm()
return render(request, 'UserSignUp/signup.html', {'form': form})
def account_activation_sent(request):
return render(request, 'UserSignUp/account_activation_sent.html')
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.profile.email_confirmed = True
user.save()
login(request, user)
return redirect('index')
else:
return render(request, 'UserSignUp/account_activation_invalid.html')
The signup.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Signup</title>
</head>
<body>
<header>
<h1>My Site</h1>
{% if user.is_authenticated %}
logout
{% else %}
login / signup #<----- here it encounters error
{% endif %}
<hr>
</header>
<main>
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
</main>
</body>
</html>
You need to specify the name of login and logout views in urls.py
urlpatterns = [
# rest of your URL configs
path('login/', auth_views.LoginView.as_view(template_name='login/login.html', redirect_field_name='index'),
name='login'),
path('logout', auth_views.LogoutView.as_view(template_name='logout/logout.html', next_page='index'),
name='logout'),
]

Django redirecting to incorrect URL

I am working on the login/logout functionality of a basic Django website. (CS50's Pinocchio's Pizza). The logout path is being injected into the URL & I am unsure as to why. My navbar links to the home page, yet when I click on it it redirects me to the logout page. Any other link I click, that link's path is added to the URL but attached to the logout path.
For example, clicking on the login button of my site, whose path is login_default, the url becomes:
http://127.0.0.1:8000/logoutlogin_default
Trying to click the link in the navbar that should link to the index page gets me:
http://127.0.0.1:8000/logout
The folder "orders" is an app which "pizza" is made aware of. All html pages are inside orders/templates/orders.
This is orders/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name = "index"),
path("register_default", views.register_default, name = "register_default"),
path("register_setup", views.register_setup, name = "register"),
path("login_default", views.login_default, name = "login_default"),
path("login_setup", views.login_setup, name = "login"),
path("logout", views.logout_view, name="logout"),
]
In pizza/urls.py, the Orders app's URLs have been made known:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", include("orders.urls")),
path("register_default", include("orders.urls")),
path("register_setup", include("orders.urls")),
path("login_default", include("orders.urls")),
path("login_setup", include("orders.urls")),
path("logout", include("orders.urls")),
path("admin/", admin.site.urls),
]
This is orders/views.py
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
# Create your views here.
def index(request):
# If user is not logged in
if not request.user.is_authenticated:
return render(request, "orders/index.html", {"message": None})
context = {
"user": request.user
}
return render(request, "orders/index.html", context)
def register_default(request):
return render(request, "orders/register.html")
def register_setup(request):
firstName = request.POST["userFirstName"]
lastName = request.POST["userLastName"]
email = request.POST["userEmail"]
pw = request.POST["userPW"]
user = User.objects.create_user(firstName, email, pw)
user.save()
return HttpResponseRedirect(reverse("index"))
def login_default(request):
return render(request, "orders/login.html")
def login_setup(request):
username = request.POST["loginName"]
password = request.POST["loginPW"]
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "orders/login.html", {"message": "Invalid Credentials"})
def logout_view(request):
logout(request)
return render(request, "orders/login.html", {"message": "Logged Out"})
This is base.html, which all other templates are based off of.
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}{% endblock %}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-md bg-light">
Pinocchio's Pizza
<ul class="navbar-nav">
{% if message %}
<li class="nav-item">
Log Out
</li>
{% else %}
<li class="nav-item">
Log In
</li>
<li class="nav-item">
Sign Up
</li>
{% endif %}
</ul>
</nav>
{% block body %}{% endblock %}
</body>
</html>
This is login.html
{% extends "orders/base.html" %}
{% block title %}Log In{% endblock %}
{% block body %}
{% if message %}
<h1 class="text-danger">{{ message }}</h1>
{% endif %}
<div class="container-fluid">
<div class="row">
<div class="col-md-6 mx-auto mt-4">
<form action="{% url 'login' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="loginName">First Name</label>
<input type="text" name="loginName" id="loginName" class="form-control">
</div>
<div class="form-group">
<label for="loginPW">Password</label>
<input type="password" class="form-control" name="loginPW" id="loginPW">
</div>
<button class="btn btn-success" type="submit">Log In</button>
</form>
</div>
</div>
</div>
{% endblock %}
This is pizza/settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'i0&iq&e9u9h6(4_7%pt2s9)f=c$kso=k$c$w#fi9215s=1q0^d'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'orders.apps.OrdersConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'pizza.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'pizza.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
Try changing pizza/urls.pyto the following:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("", include("orders.urls")),
path("admin/", admin.site.urls),
]
the reason is the "" path should automatically include all the urls in orders.urls. Here is a link to the documentation: https://docs.djangoproject.com/en/2.2/topics/http/urls/#including-other-urlconfs
Another thing that I noticed that helps catch bugs is to include a / at the end of urls so in your orders/urls.py you can change it to the following.
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name = "index"),
path("register_default/", views.register_default, name = "register_default"),
path("register_setup/", views.register_setup, name = "register"),
path("login_default/", views.login_default, name = "login_default"),
path("login_setup/", views.login_setup, name = "login"),
path("logout/", views.logout_view, name="logout"),
]

Django 2.0 Tutorial - AttributeError at /polls/1/vote/ 'ReverseManyToOneDescriptor' object has no attribute 'get'

I've come across this error on SE before, but it tends to be for previous versions of Django - and I don't have a good understanding of how Django works. I've made it to part 4 of the tutorial, and it was working up until this tutorial. I'm really not sure what I am doing wrong - most of this is copy/pasted from the tutorial at this point to see if that would make it work. I'm not exactly python saavy so if someone could explain the problem in fairly simple english I would appreciate it. I know the issue has something to do w/ line 30 in veiws, in the "vote" section -
def vote(request, question_id):
try:
selected_choice = Question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
Thanks SE.
error code:
AttributeError at /polls/1/vote/
'ReverseManyToOneDescriptor' object has no attribute 'get'
Request Method: POST
Request URL: http://localhost:8000/polls/1/vote/
Django Version: 2.0
Exception Type: AttributeError
Exception Value:
'ReverseManyToOneDescriptor' object has no attribute 'get'
Exception Location: /Users/coreydickinson/mysite/DJDev/env1/mysite2/mysite3/polls/views.py in vote, line 30
Python Executable: /Users/coreydickinson/mysite/DJDev/env1/bin/python
Python Version: 3.6.5
Python Path:
['/Users/coreydickinson/mysite/DJDev/env1/mysite2/mysite3',
'/Users/coreydickinson/mysite/DJDev/env1/lib/python36.zip',
'/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6',
'/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6/site-packages']
Server time: Sat, 26 May 2018 03:16:49 +0000
Environment:
Request Method: POST
Request URL: http://localhost:8000/polls/1/vote/
Django Version: 2.0
Python Version: 3.6.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/Users/coreydickinson/mysite/DJDev/env1/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/coreydickinson/mysite/DJDev/env1/mysite2/mysite3/polls/views.py" in vote
30. selected_choice = Question.choice_set.get(pk=request.POST['choice'])
Exception Type: AttributeError at /polls/1/vote/
Exception Value: 'ReverseManyToOneDescriptor' object has no attribute 'get'
heres my code for the various files:
polls/urls:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/veiws:
from django.urls import reverse
from django.views import generic
from .models import Choice, Question
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 Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/details.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
try:
selected_choice = Question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
polls/admin:
from django.contrib import admin
from .models import Question
admin.site.register(Question)
polls/urls:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/templates/polls/details.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
polls/templates/polls/results.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
Vote again?
polls/templates/polls/index.html:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
mysite/settings.py
"""
Django settings for mysite3 project.
Generated by 'django-admin startproject' using Django 2.0.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*8a#se$l_8v%(-xyz=!!=jgvyjyn611zqw(dqroejvkk^o9%co'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mysite3.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mysite3.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
This is the line throwing the error:
selected_choice = Question.choice_set.get(pk=request.POST['choice'])
Basically, you need to call choice_set on an instance of the Question model, not on the model itself.
So something like this might work:
question = Question.objects.get(pk=question_id)
selected_choice = question.choice_set.get(pk=request.POST['choice'])

Django modelformset_factory save nothing

Created a set of forms for a simple model. When I am try to change the model object data in the form and save these changes in the database, the new data is not saved as a result, and a redirect to the form page with the same data occurs, although at least messages about the success or failure of the operation should be output. The terminal does not return any errors, it is written in the logs that the correct request was sent, the test server works without drops in normal mode. There is an opinion that the reason for this is a refusal to validate, but for what reason this happens and where exactly the error is hidden, it is not yet possible to understand.
And
When I clicked a "save" button, see this message in terminal (see below):
[25/Aug/2017 18:03:06] "GET
/categories/?csrfmiddlewaretoken=igSZl3z8pcF9qRGaMts9hG3T9dyaIpVvAxB672R34bmKvGYd6pymjmtwyEgDHGg2&form-TOTAL_FORMS=6&form-INITIAL_FORMS=5&form-MIN_NUM_FORMS=0&form-MAX_NUM_FORMS=1000&form-0-id=1&form-0-name=fhdrhddh&form-0-order=0&form-1-id=2&form-1-name=gdegasf&form-1-order=6&form-2-id=3&form-2-name=dfdgbsbgsdgs&form-2-order=2&form-3-id=4&form-3-name=dbgsgbasedgbaedvg&form-3-order=3&form-4-id=5&form-4-name=dgfsdg3waesdvz&form-4-order=4&form-5-id=&form-5-name=&form-5-order=0
HTTP/1.1" 200 7502
models.py (categories app)
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length = 30, db_index = True, unique = True, verbose_name = "Title")
order = models.PositiveSmallIntegerField(default = 0, db_index = True, verbose_name = "Serial number")
def __str__(self):
return self.name
class Meta:
ordering = ["order", "name"]
verbose_name = "category"
verbose_name_plural = "categories"
views.py (categories app)
from django.views.generic.base import TemplateView
from django.forms.models import modelformset_factory
from django.shortcuts import redirect
from django.contrib import messages
from categories.models import Category
from generic.mixins import CategoryListMixin
CategoriesFormset = modelformset_factory(Category, can_delete=True, fields = '__all__', extra=1, max_num=None)
class CategoriesEdit(TemplateView, CategoryListMixin):
template_name = "categories_edit.html"
formset = None
def get(self, request, *args, **kwargs):
self.formset = CategoriesFormset()
return super(CategoriesEdit, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(CategoriesEdit, self).get_context_data(**kwargs)
context["formset"] = self.formset
return context
def post(self, request, *args, **kwargs):
self.formset = CategoriesFormset(request.POST)
if self.formset.is_valid():
self.formset.save()
messages.api.add_message(request, messages.SUCCESS, "List of categories successfully changed")
return redirect("categories_edit")
else:
messages.api.add_message(request, messages.SUCCESS, "Something is wrong!!!")
return super(CategoriesEdit, self).get(request, *args, **kwargs)
mixins.py
from django.views.generic.base import ContextMixin
class CategoryListMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super(CategoryListMixin, self).get_context_data(**kwargs)
context["current_url"] = self.request.path
return context
categories_edit.html (categories app)
{% extends "categories_base.html" %}
{% block title %} Categories {% endblock %}
{% block main %}
{% include "generic/messages.html" %}
{{ formset.errors }}
<h2>Categories</h2>
<form action="" method="post">
{% include "generic/formset.html" %}
<div class="submit-button"><input type="submit" value="Save"></div>
</form>
{% endblock %}
formset.html (categories app)
{% csrf_token %}
{{ formset.management_form }}
<table class="form">
<tr>
<th></th>
{% with form=formset|first %}
{% for field in form.visible_fields %}
<th>
{{ field.label }}
{% if field.help_text %}
<br>{{ field.help_text }}
{% endif %}
</th>
{% endfor %}
{% endwith %}
</tr>
{% for form in formset %}
<tr>
<td>
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
</td>
{% for field in form.visible_fields %}
<td>
{% if field.errors.count > 0 %}
<div class="error-list">
{{ field.errors }}
</div>
{% endif %}
<div class="control">{{ field }}</div>
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
messages.html (categories app)
{% if messages %}
<div id="messages-list">
{% for message in messages %}
<p class="{{ message.tags }}">{{ message }}</p>
{% endfor %}
</div>
{% endif %}
urls.py (categories app)
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from categories.views import CategoriesEdit
urlpatterns = [
url(r'^$', login_required(CategoriesEdit.as_view()), name = "categories_edit"),
]
settings.py (project)
"""
Django settings for t****** project.
Generated by 'django-admin startproject' using Django 1.10.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '***************************************************'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'guestbook',
'categories',
'imagepool',
'page',
'main',
'news',
'shop',
'django.contrib.sites',
'django_comments',
'easy_thumbnails',
'taggit',
'precise_bbcode',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 't******.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'tdkennel.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 't******l',
'USER': 'p*******',
'PASSWORD': '********',
'HOST': '',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')
MEDIA_URL = '/media/'
LOGIN_URL = "login"
LOGOUT_URL = "logout"
SITE_ID = 1
THUMBNAIL_BASEDIR = "thumbnails"
THUMBNAIL_BASEDIR = {"goods.Good.image": {"base": {"size": (200, 100)},},}
LOGIN_REDIRECT_URL = "main"
I recall that something similar happened to me. I ended up using a Form to create the Formset:
try:
from django.forms import ModelForm
from categories.models import Category
from django.forms.models import modelformset_factory
class CategoryForm(ModelForm):
class Meta:
model = Category
fields = '__all__'
CategoryFormSet = modelformset_factory(Category, form=CategoryForm)
and use CategoryFormSet in CategoriesEdit
It's just a workaround, but hope this helps!
My inattention was my mistake. I wrote method="post" in the commented and concatenated part of the code of categories_edit.html (in my project) and forgot to write it in uncommented piece of code:
{# <form method="post" action=""> #}
<form method="" action="">
{% include "generic/formset.html" %}
{# {% csrf_token %} #}
{# {{ formset.as_p }} #}
<div class="submit-button"><input type="submit" value="Save"></div>
</form>
But I confused the users, because wrote everything is correct in my Question, than delete concatenated part of the code and entered the correct piece of code:
<form method="post" action="">
{% include "generic/formset.html" %}
{# {% csrf_token %} #}
{# {{ formset.as_p }} #}
<div class="submit-button"><input type="submit" value="Save"></div>
</form>
I apologize to the users who responded to my question! Code working.

Categories