I am trying to catch all URLs from the safer app and send them to a catch all view; for example:
http://127.0.0.1:8000/saferdb/123
http://127.0.0.1:8000/saferdb/12
I think I have an issue with my reg ex in url.py:
from django.conf.urls import url
from . import views
app_name = 'saferdb'
urlpatterns = [
url(r'^/$', views.index, name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.detail, name='detail'),
]
Views.py is the sample code from the django tutorial:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.all()[:5]
output = ', '.join([q.DOT_Number for q in latest_question_list])
return HttpResponse(output)
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
I noticed that /saferdb/ will not work unless the reg ex contains a slash:
r'^/$' instead of r'^$' as shown in the django tutorial.
Please add '/' at the end of url in root urls.py of 'safer' app, something similar to this:
url(r'^saferdb/', include('saferdb.urls', namespace='saferdb'))
Related
Using the URLconf defined in todo.urls, Django tried these URL patterns, in this order:
^admin/
^todoapp/
The empty path didn't match any of these.
404 error is showing when I run my code which you can find below. This code is app url:
from django.contrib import admin
from django.conf.urls import url
from todoapp import views
urlpatterns = [
#url(r'^admin/', admin.site.urls),
url(r'^$',views.index,name='index'),
#todoapp/1
url(r'^(?P<task_id>[0-9]+)/$',views.detail, name='detail'),
]
And it is the main url.py
from django.contrib import admin
from django.conf.urls import url, include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^todoapp/',include('todoapp.urls')),
]
views.py is here:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Task
from django.template import loader
# Create your views here.
def index(request):
task_list = Task.objects.all()
template = loader.get_template('todoapp/index.html')
context = {
'task_list': task_list
}
return render(request, 'todoapp/index.html',context)
def detail(request, task_id):
task = Task.objects.get(pk=task_id)
context = {
'task': task,
}
return render(request,'todoapp/detail.html',context)
Why it is not working, I could not find a problem. How can I fix it?
I'm doing tutorial "Movie rental" and i got error.
Using the URLconf defined in vidly.urls, Django tried these URL patterns, in this order:
Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.
my code is(from main urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('movies/', include('movies.urls'))
]
from mysite i.e movies(urls.py)
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='movie_index'),
path('<int:movie_id', views.detail, name='movie_detail')
]
from views.py
from django.shortcuts import render
from django.http import HttpResponse
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)
What i'm doing wrong?
In your movies.urls you forgot to close URL parameter.
path('<int:movie_id>/', views.detail, name='movie_detail')
Instead you did,
path('<int:movie_id', views.detail, name='movie_detail')
You're missing a closing >/.
path('<int:movie_id>/', views.detail, name='movie_detail')
I am trying to show a blogpost on the site.
Below is details of urls and view file details but still it showing a 404 not found page
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="ShopHome"),
path("blogpost/", views.blogpost, name="blogpost")
]
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request, 'blog/index.html')
def blogpost(request):
return render(request, 'blog/blogpost.html')
Showing:
404 not found page
you should include urls.py of your application to project urls.py
actually if your project name is "mysite" and your application name is "blogspot", you should include mysite/blogspot/urls.py on this file mysite/mysite/urls.py like this:
from django.urls import path, include
urlpatterns = [
path('', include('blogspot.urls')),
]
Actually, i forget to add the slash at the time of registering the url of this app in urls
I'm following the Django introductory tutorial, and I'm running into a weird error. Or at least I think it's a weird error.
I'm on part 3, which is writing more views. I have, to the best I can tell, followed the tutorial to the letter.
My /polls/urls.py file looks like this:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results,
name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
if __name__ == "__main__":
pass
And polls/views.py looks like this:
from django.http import HttpResponse
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 {question}.".format(question=question_id))
def results(request, question_id):
response = "You're looking at the results of question {question}.".format(question=question_id)
return HttpResponse(response)
def vote(request, question_id):
return HttpResponse("You're voting on question {question}.".format(question=question_id))
if __name__ == "__main__":
pass
And I've registered the urls in my_project/urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls', include("polls.urls"))
]
If I go to http://127.0.0.1:8000/polls I see the "hello world" message that I expect to see, but it I try looking up one of the questions, i.e. I go to http://127.0.0.1:8000/polls/1/ I see the following error message:
Using the URLconf defined in learning_django.urls, Django tried these URL patterns,
in this order:
1. ^admin/
2. ^polls ^$ [name='index']
3. ^polls ^(?P<question_id>[0-9]+)/$ [name='detail']
4. ^polls ^(?P<question_id>[0-9]+)/results/$ [name='results']
5. ^polls ^(?P<question_id>[0-9]+)/vote/$ [name='vote']
The current URL, polls/1/, didn't match any of these.
How is it possible that my url doesn't match number 3? It's such a basic regex.
The problem is your my_project/urls.py, after polls, you missed /, change to :
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include("polls.urls")) # add / after polls
]
You can either use the Django APPEND_SLASH setting (see here for documentation) or modify your URL pattern to make the / optional, e.g. like this:
url(r'^(?P<question_id>[0-9]+)/?$', views.detail, name='detail'),
So I'm going through the Django tutorial, using version 1.8
When I go to the page that should display the index, which is at
http://127.0.0.1:8000/
I get a 404 error which says
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in forumtest.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these.
These are my urls pages.
forumtest/urls.py :
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls',namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
]
I'm also creating an app called polls with it's own urls.py
polls/urls.py :
from django.conf.urls import url
from . import views
urlpatterns = [
# ex: /polls/
url(r'^$', views.IndexView.as_view(), name='index'),
# ex: /polls/5/
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
# ex: /polls/5/results/
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
This is my views page :
polls/views.py :
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponseRedirect,HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Choice,Question
# Create your views here.
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/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request,question_id):
p = get_object_or_404(Question, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except(KeyError,Choice.DoesNotExist):
#redisplay the question voting form
return render(request,'polls/detail.html',{
'question':p,
'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=(p.id,)))
I have no idea how to fix an issue like this, so if anyone can help that would be great.
Thanks.
The error message shows you what is wrong. You have defined urls under /polls/, so that is the address you should go to.