Django URLConf Not Resolving Properly - python

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'),

Related

Django FirstApp

I am following the django tutorial (found here):
views.py code:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
urls.py code (this is inside polls app urls.py file):
from django.urls import path, include
from django.conf import settings
from . import views
urlpatterns = [path(r'^$', views.index, name='index'), ]
urls.py code ( this is root urls.py file code):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [path('polls/', include('polls.urls')),path('admin/',admin.site.urls), ]
Here is my run command : python manage.py runserver 8080
I tried to run it today, but I am getting the following error:
Page not found (404)
Request Method: GET
Request URL: http://35527a91f40c4e228d6c464d8a8c8487.vfs.cloud9.eu-west-1.amazonaws.com/
Using the URLconf defined in PollApp.urls, Django tried these URL patterns, in this order:
poll/
admin/
The empty path 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.
You are using regex-syntax with path, you should use the empty string instead:
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Furthermore, you use a poll/ prefix here, so that means you either need to visit localhost:8000/poll/, or change this to an empty prefix:
urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]
Djano errors are self-explanatory, follow its instructions, and are good to go.Also is there the Indentation in your first return ?
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

i am trying to learn django from django documentation but i am getting an error message in the first part of the documentation

Using the URLconf defined in blogs.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
this is the error i am getting after i wrote the following code:
for urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
for polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
3.for polls/views.py
"""
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
"""
how i can i remove this error in vs code

Using the URLconf defined,Django tried these URL patterns

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')

How to configure Django views and urls to render specific templates

When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.

Page not found in Django

I'm following this tutorial to learn Django. I'm a total starter.
My urls.py file has following code
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
My views.py file is
from django.shortcuts import render
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
When i try to access th url http://127.0.0.1:8000/polls/ in my system it gives a page not found message. What am i doing wrong?
Is it a problem with the version difference?
Here is the screenshot of the error
This error comes if your urls.py file in polls directory does not have this pattern. Create a file urls.py your polls directory and add following url pattern
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
)
Ok, so if you want to use /polls/ with this urls,
your polls.urls should look similar to this ( for django1.8):
from django.conf.urls import url
from polls.views import BasePollView, AnotherPollView
urlpatterns = [
url(r'^$', BasePollView.as_view()),
url(r'^another-url/$', AnotherPollView.as_view())
]
/polls/ -> BasePollView
/polls/another-url/ -> AnotherPollView

Categories

Resources