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.
Related
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 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'm working on a bilingual website French and English, and I'm trying to figure out how to detect and redirect the French users to index_fr.html (in templates folder).
My urls.py (it only shows the english ver. for now):
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', profiles.views.home, name='home'),
]
Thanks for your responses.
As dhke said, I enabled LocalMiddleware and used request.LANGUAGE_CODE to write a function in views.py that renders a template depending on the language and it works!:
views.py:
from django.http import HttpResponse
def home(request):
context={}
if request.LANGUAGE_CODE == 'fr':
template='index_fr.html'
else:
template='index_en.html'
return render(request,template,context)
def fr(request):
context={}
template='index_fr.html'
return render(request,template,context)
def all(request):
context={}
template='index_en.html'
return render(request,template,context)
urls.py:
urlpatterns =[
url(r'^admin/', admin.site.urls),
url(r'^$', profiles.views.home, name='home'),
url(r'^index_fr.html', profiles.views.fr, name='fr'),
url(r'^index_en.html', profiles.views.all, name='all'),
]
I've created a project from official docs. Everything is same but still I am not able to communicate with my root urls.py through my app urls.py.
If I use views to redirect to app urls.py they don't work. But same if do with root urls.py it works fine.
My settings are:
ROOT_URLCONF = 'mysite.urls'
root urls:
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/$', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
app urls.py:
from django.conf.urls import url
from polls import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
Simple view I am trying to present is:
from django.shortcuts import render, HttpResponse, RequestContext, loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'index.html', context)
Please help
After removing the dollar from polls/$, I get a new error:
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:
^polls/
^admin/
^static\/(?P<path>.*)$
^media\/(?P<path>.*)$
The current URL, , 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.
Remove the dollar sign from the url that is including the polls urls.
url(r'^polls/', include('polls.urls')),
I am new in Python, I have a problem with the admin URL.
When I type
localhost:8000/admin/
My browser tells me:
DoesNotExist at /admin/
Question matching query does not exist
My index page and detail pages work fine.
I thought it was a mistake in the order of URL's,
I changed the order of the admin and detail URL,
but still nothing.
Can somebody please give me a hint.
project/urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<book_title>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
books/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all
context = {'latest_question_list': latest_question_list}
return render(request, 'books/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})
I forgot to put parentheses for Question.objects.all So silly of me.
add this before urlpatterns in urls.py
admin.autodiscover()