I'm making the Django tutorial at the official website and I'm currently setting up URLs for the sample polling application we're creating.
As of now, my polls/urls.py 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.votes, name='vote')
]
And I can't help but notice the repetition of (?P<question_id>[0-9]+) so I wonder if there's a simpler way to avoid this besides extracting it to a constant like QUESTION_PATTERN = (?P<question_id>[0-9]+)
You can include a list of subpatterns, like this:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/', include([
url(r'^$', views.detail, name='detail'),
url(r'^results/$', views.results, name='results'),
url(r'^vote/$', views.votes, name='vote'),
]),
]
Related
urls.py
from django.urls import path from.import views
urlpatterns = [
path('', views.index, name='index'),
path('about', views.about, name='about'),
path('ourwork', views.ourwork, name='ourwork'),
path('portfolio', views.portfolio, name='portfolio'),
path('blog', views.blog, name='blog'),
path('careers', views.careers, name='careers'),
path('contact', views.contact, name='contact'),
]
views.py
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'artsoft/index.html')
def about(request):
return render(request,'artsoft/about.html')
def ourwork(request):
return render(request,'artsoft/ourwork.html')
def portfolio(request):
return render(request,'artsoft/portfolio.html')
def blog(request):
return render(request,'artsoft/blog.html')
def careers(request):
return render(request,'artsoft/careers.html')
def contact(request):
return render(request,'artsoft/contact.html') `
screen shot
The Error page
but when i clicking on blog this is work
Blog page
views.py
urls.py
directories of files
That because you /about/ has a slash at the end but /blog didn't. You could go with this:
path('', views.index, name='index'),
path('about/', views.about, name='about'),
path('ourwork/', views.ourwork, name='ourwork'),
path('portfolio/', views.portfolio, name='portfolio'),
path('blog/', views.blog, name='blog'),
path('careers/', views.careers, name='careers'),
path('contact/', views.contact, name='contact'),
and by default, Django has APPEND_SLASH=True, with this setting Django will add a slash at the end of your url so domain.com/blog and other paths which have no slash at the end also work as normal
As I can see where it worked (blog), you didn't add the last one /
One solution is
from django.urls import path from.import views
urlpatterns = [
path('/', views.index, name='index'),
path('about/', views.about, name='about'),
path('ourwork/', views.ourwork, name='ourwork'),
path('portfolio/', views.portfolio, name='portfolio'),
path('blog/', views.blog, name='blog'),
path('careers/', views.careers, name='careers'),
path('contact/', views.contact, name='contact'),
]
Or in the navigator put
127.0.0.1:8000/blog
127.0.0.1:8000/contact
127.0.0.1:8000/about
without the last /
These errors occur when you enter the admin page. I want to fix this. Help me.
problementer image description here
Source code
urls.py-fistsite
from django.contrib import admin
from django.urls import path, include
from polls import views
urlpatterns = [
path('', views.index, name='index'),
path('/polls', include('polls.urls')),
path('/admin', include('polls.urls'))
]
urls.py-polls
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/result/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote')
]
Change your urls.py to this and it should work.
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls), # add this
path('', views.index, name='index'),
]
For some reason you changed the site admin's url to include('polls.urls') which is incorrect. Change it back to path('admin/', admin.site.urls) and django will pick it up. And on the side note, You don't have to add / django has a middleware that does it automatically.
In my django project, when I access localhost:8000 it says:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
The urls.py is:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace="polls")),
]
The polls urls.py is:
from django.conf.urls import url, include
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^admin/', admin.site.urls),
]
The Django version is 1.10. Can anyone help me identify the problem? Thanks in advance.
You do not have a route for / it seems, so http://localhost:8000/ does not get you anywhere.
You wrote url(r'^polls/', include('polls.urls', namespace="polls")), in urls.py so all the routes defined in polls.urls are to be prefixed with polls/.
You may actually want to go to http://localhost:8000/polls/ (notice the polls/, because the route you defined as index is listed in the polls app.
Had you wanted to route your polls index to your url root, you should change urls.py to something like
urlpatterns = [
url(r'^', include('polls.urls', namespace="polls")),
url(r'^admin/', admin.site.urls),
]
and the forget about the polls/ part in the urls.
In main urls.py change
from
url(r'^polls/', include('polls.urls', namespace="polls")),
to
url(r'^$', include('poll.urls')),
If I setup DRF documetation with include_docs_urls, HTML page will render only modules, explicitly indicated in project's urls.py.
But all modules, added to urlpatterns with django.conf.urls.include function, HTML renderer will ignore it. But coreapi-cli gets right schema.
If code is:
api_patterns = [
url(r'^api-token-refresh/$', refresh_jwt_token),
url(r'^api-token-verify/$', verify_jwt_token),
]
urlpatterns = [
url(r'^$', HTMLReport.as_view()),
url(r'^admin/', admin.site.urls),
url(r'^vending/', include('vending.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/$', obtain_jwt_token),
url(r'^api-tools/$', include(api_patterns)),
url(r'^docs/', include_docs_urls(title='TMS API', description='Terminal Management System API', public=True))
]
HTML would ignore docs for vending.urls, and api_patterns.
But if code is:
urlpatterns = [
url(r'^$', HTMLReport.as_view()),
url(r'^admin/', admin.site.urls),
url(r'^vending/', include('vending.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/$', obtain_jwt_token),
url(r'^api-token-refresh/$', refresh_jwt_token),
url(r'^api-token-verify/$', verify_jwt_token),
url(r'^docs/', include_docs_urls(title='TMS API', description='Terminal Management System API', public=True))
]
Only vending.urls are ignored.
Instead of
url(r'^api-tools/$', include(api_patterns)),
Try to use concatenation of url_patterns:
urlpatterns + api_patterns
and change the position of
url(r'^vending/', include('vending.urls')),
to this way:
urlpatterns = [
url(r'^$', HTMLReport.as_view()),
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/$', obtain_jwt_token),
url(r'^docs/', include_docs_urls(title='TMS API', description='Terminal Management System API', public=True))
url(r'^vending/', include('vending.urls')),
]
urlpatterns + api_patterns
If still vending.urls is ignored then try to use namespace.
I've been racking my brains and can't figure out why there should be an import error when 'views' is imported. I get the following message when I visit my index page:
"
Request Method: GET
Request URL: http://127.0.0.1:8000/moments/
Django Version: 1.6.1
Exception Type: ImportError
Exception Value:
No module named views
Exception Location: C:\Python27\lib\site-packages\django\utils\importlib.py in import_module, line 40
"
Here is my urls.py
from django.conf.urls import patterns, url
from moments_app import views
urlpatterns = patterns('',
url(r'^$', "views.index", name='index'),
url(r'^$', "views.choose_dataset", name='choose'),
url(r'^get_moments/', "views.get_moments", name='get_moments'),
url(r'^learn/$', "views.learn", name='learn'),
url(r'^(?P<moment_id>\d+)/$', "views.detail", name='detail'),
)
I clearly have a module named views in my moments_app folder. Also, moments_app is in my path. Does anyone have any ideas as to what might be causing this?
You prefixed your route names with a relative module name. Use an absolute name:
urlpatterns = patterns('',
url(r'^$', "moments_app.views.index", name='index'),
url(r'^$', "moments_app.views.choose_dataset", name='choose'),
url(r'^get_moments/', "moments_app.views.get_moments", name='get_moments'),
url(r'^learn/$', "moments_app.views.learn", name='learn'),
url(r'^(?P<moment_id>\d+)/$', "moments_app.views.detail", name='detail'),
)
or better still, use the first argument to specify the full module path:
urlpatterns = patterns('moments_app.views',
url(r'^$', "index", name='index'),
url(r'^$', "choose_dataset", name='choose'),
url(r'^get_moments/', "get_moments", name='get_moments'),
url(r'^learn/$', "views.learn", name='learn'),
url(r'^(?P<moment_id>\d+)/$', "detail", name='detail'),
)
although a combination of the two is also allowed:
urlpatterns = patterns('moments_app',
url(r'^$', "views.index", name='index'),
url(r'^$', "views.choose_dataset", name='choose'),
url(r'^get_moments/', "views.get_moments", name='get_moments'),
url(r'^learn/$', "views.learn", name='learn'),
url(r'^(?P<moment_id>\d+)/$', "views.detail", name='detail'),
)
Two year update:
In Django 1.8 and later both string views and the patterns function are deprecated, resulting in a simpler and more reliable syntax:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.choose_dataset, name='choose'),
url(r'^get_moments/', views.get_moments, name='get_moments'),
url(r'^learn/$', views.learn, name='learn'),
url(r'^(?P<moment_id>\d+)/$', views.detail, name='detail'),
]
Note that there are no "relative" or "absolute" view names with the callable syntax -- if you import the views module you get its definitions. I'd avoid importing the individual view functions since there's a tiny chance that another import could define a colliding name. If you're not worried about collisions and don't mind putting your app name in the file, the urls can be slightly shortened:
from moments_app.views import index, choose_dataset, get_moments, learn, detail
urlpatterns = [
url(r'^$', index, name='index'),
url(r'^$', choose_dataset, name='choose'),
url(r'^get_moments/', get_moments, name='get_moments'),
url(r'^learn/$', learn, name='learn'),
url(r'^(?P<moment_id>\d+)/$', detail, name='detail'),
]
You have imported your view as
from moments_app import views
Some times it won't work.
Use this
from moments_app.views import *
urlpatterns = patterns('',
url(r'^$', index, name='index'),
url(r'^$', choose_dataset, name='choose'),
url(r'^get_moments/', get_moments, name='get_moments'),
url(r'^learn/$', learn, name='learn'),
url(r'^(?P<moment_id>\d+)/$', detail, name='detail'),
)
It will work..
Just Change import statement to
import appname.views
This works fine for my code.
I faced this problem and tried above answers but the issue was that i was missing a string quote in my "render" function in views.py which i missed and was facing issue in urls.py which states that no module "views" is present in "portfolio"(app name).
Hope this helps!