I am following the tutorial on the Django website. I try and replicate this:
My code is as follows:
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, film_id):
return HttpResponse("You're looking at film %s." % film_id)
def results(request, film_id):
response = "You're looking at the results of film %s."
return HttpResponse(response % question_id)
def vote(request, film_id):
return HttpResponse("You're commenting on film %s." % film_id)
films/urls.py
from django.conf.urls import url
from django.urls import path
from . import views
urlpatterns = [
# url(r'^$', views.index, name='index'),
# ex: /polls/
path('', views.index, name='index'),
# ex: /films/5/
path('<int:film_id>/', views.detail, name='detail'),
# ex: /films/5/results/
path('<int:film_id>/results/', views.results, name='results'),
# ex: /films/5/vote/
path('<int:film_id>/vote/', views.vote, name='vote'),
]
With this I am getting ERR_CONNECTION_REFUSED. If I comment out all the paths leaving only the index url, and also comment out from django.urls import path a page displays, but that is where I was at before trying to add more views.
You are referring to the documentation for newer Django version as path() did not exist in older versions. You can select the documentation version by clicking on the Documentation version button at the bottom right here.
Related
ok polls tutorial is done and can enter questions, vote, etc. but from the admin pages View site aka localhost:8000 gives...
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
dose/
admin/
The empty path didn’t match any of these.
Now I have polls/views.py...
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils import timezone
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 (not including those set to be
published in the future).
"""
return Question.objects.filter(
pub_date__lte=timezone.now()
).order_by('-pub_date')[:5]
and mysite/urls.py set to ...
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
and polls/urls.py set to...
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'),
]
The only hint I can find is that all the imports show error highlights in VSCode with the Error "reportMissingModuleSources"
But python and django run perfectly well everywhere but index.html
No url at http://localhost:8000/ address is expected behaviour, where you want to go is http://localhost:8000/polls/.
If you want go directly to the desired View without writing polls/ prefix you should define your urls this way:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
So you place path() call without url prefix. Generally if you include any url group you define the beginning of address this way.
I am currently on Django documentation part 3 where I have to create the polls app. However, when I make a request to the url having path like http://127.0.0.1:8000/polls/3/, I was supposed to get the message 'Question does not exist.' But I don't.
Here is my code
polls/views.py
from django.http import HttpResponse, HttpResponseNotFound, Http404
from django.shortcuts import render
from .models import Question
# Create your views here.
def index(request):
# - sign to get more recent, list is ordered from 0 to 4
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'latest_question_list': latest_question_list,
}
return render(request, 'polls/index.html', context)
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
return HttpResponseNotFound("Question does not exist")
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
The error is
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/3/
Raised by: polls.views.detail
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
polls/ [name='index']
polls/ <int:question_id>/ [name='detail']
The current path, polls/3/, matched the last one.
Try changing raise Http404("Question does not exist") to return HttpResponseNotFound("Question does not exist")
You can set debug=false , provide a message to Http404 and then it will appear in the standard 404 debug template. Find more details here.
I am doing this Django project and I am stuck at this. I have created a new url path which will return the value which will be given after movies/__ in url.
here is my code:
note: movie is one the app in my application
movies/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="movie_index"),
path("< movie_id >", views.detail, name="movie_detail")
]
movies/views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import movie
def index(request):
movies = movie.objects.all()
return render(request, "movies/index.html", {"movies": movies})
def detail(request, movie_id):
return HttpResponse(movie_id)
views.py in root folder
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
my error :
enter image description here
I am using Django 2.1 and python 3.8
thanks in advance.......
path("<int:movie_id>", views.detail, name="movie_detail")
instead of
path("< movie_id >", views.detail, name="movie_detail")
I am creating an Django app and am facing issues with routes not being identified. It is the same DJango poll app I am trying to create but the documentation code does not work. Here is my code below:
djangoproject/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^simpleapp/', include('simpleapp.urls')),
url(r'^admin/', admin.site.urls),
]
simpleapp/views.py
from django.shortcuts import render
from django.http import HttpResponse, request
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
simpleapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /simpleapp/
# url('', views.index, name='index'),
# ex: /simpleapp/5/
url('<int:question_id>/', views.detail, name='detail'),
# ex: /simpleapp/5/results/
url('<int:question_id>/results/', views.results, name='results'),
# ex: /simpleapp/5/vote/
url('<int:question_id>/vote/', views.vote, name='vote'),
]
If I un-comment the first url of '' path of simpleapp/urls.py code, all the pages shown are '' path. However, if I keep the url '' path commented, then the routes give me the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/simpleapp/34/
Using the URLconf defined in simple_django.urls, Django tried these URL patterns, in this order:
^simpleapp/ <int:question_id>/ [name='detail']
^simpleapp/ <int:question_id>/results/ [name='results']
^simpleapp/ <int:question_id>/vote/ [name='vote']
^admin/
The current path, simpleapp/34/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I was unable to get the path() imported using django.conf.urls or django.urls. url() was successful. I am using python version 3.6.7 and Django version 2.1.5. What am I missing?
You are using path syntax, not regexes, in your simpleapp/urls.py. Change it to:
urlpatterns = [
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
Your project URLs are using regexes, so you shouldn't change those, although you might want to make them consistent.
You define routes like Django 1.x
url(r'^simpleapp/', include('simpleapp.urls')),
url(r'^admin/', admin.site.urls),
This is Django 1.x type route definition
In Django 2 you must define routes like this:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
In Django 2 routes define without regular expression
See docs for route define in Django 2
I tried to go 127.0.0.1:8000/poll/ it doesn't open, but I can go to /poll/20/result.
I just add other urls of poll and base url-> /poll/ stop to be opened.
Before I add to detail,result,vote function/url there were no problem to open index of poll.
Here are my codes:
In view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You are at the poll index.")
def detail(request, poll_id):
return HttpResponse("You are looking at poll %s" %poll_id)
def results(request, poll_id):
return HttpResponse("You are looing at the results of poll %s" % poll_id)
def vote(request, poll_id):
return HttpResponse("You are voting on poll %s" %poll_id)
In Polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns( '',
url(r'^Ş', views.index, name='index'),
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', views.results,name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
In mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'sign_ups.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
In your MySite.urls,
you have mentioned the polls url as polls and given the url as poll.
Change any one of those to get the app working.