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')),
Related
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.
This should be an easy enough problem to solve for you guys:
I just started working with Django, and I'm doing some routing. This is my urls.py in the root of the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls')),
]
This is the routing in my dashboard app:
urlpatterns = [
path('dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
Now let's say I want my users to be redirected to /dashboard if they go to the root of the website. So I would use '' as a route in the urls.py in the root, and then have everyone sent to /dashboard from the urls.py in the dashboard app. But when I do this I get the following warning:
?: (urls.W002) Your URL pattern '/dashboard' [name='index'] has a route beginning with a '/'. Remove this slash as it is unnecessary. If this pattern is targeted in an include(), ensure the include() pattern has a trailing '/'.
So I tried to use '/' instead of '', but since a trailing / is automatically removed from an url, the url wouldn't match the pattern. Should I ignore/mute this warning or is there another way to go about it?
This is the code that worked perfectly but gave me a warning earlier:
urlpatterns = [
path('/dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls'))
]
You can use RedirectView to redirect from / to /dashboard/. Then use 'dashboard' when including the dashboard urls.
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name='dashboard:index')
path('dashboard/', include('dashboard.urls')),
]
You can then remove 'dashboard' from the path in dashboard/urls.py, as it is already in the include().
app_name = 'dashboard'
urlpatterns = [
path('', views.index, name='index'),
]
I've added app_name='dashboard' to match the namespace used above in pattern_name='dashboard:index'.
Note that Django projects usually use URLs with a trailing slash, e.g. /dashboard/ instead of dashboard.
If you really want to use URLs like /dashboard without a trailing slash, then the include should be
path('dashboard', include('dashboard.urls')),
If you do this, I suggest you set APPEND_SLASH to False in your settings.
You can try something like this:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contacts/', include('appname.contacts.urls')),
url(r'^comments/', include('appname.urls')),
url(r'^subscriptions/', include('appname.partner.urls')),
url(r'^', RedirectView.as_view(url="/admin/"))
]
This is what I've done in my project so whenever the user go to 127.0.0.1:8000 it redirects to /admin
I am trying to return different templates for different urls (having just one app), so basically I want to return:
One template for:http://127.0.0.1:8000/projects/
Another template for:http://127.0.0.1:8000/formpage/
I have the project urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', include('appone.urls')),
url(r'^formpage/', include('appone.urls')),
]
And the app urls.py:
from django.conf.urls import url,include
from django.http import HttpResponse
from . import views
urlpatterns = [
url(r'^$', views.projs, name='proj'),
url(r'^$', views.form_view, name='form_view')
]
I have the views and templates , that are good, but I do not understand how can I return them based on the url, because for the moment I return the first view from app urls.py, for both urls.
Make separate view functions for both urls (projs and form_view).
Then in projects/urls.py
urlpatterns = [
...
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
...
]`
Or, if you want to have separate urls.py file
projects/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^', include('appone.urls')),
]`
appone/urls.py
urlpatterns = [
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view')
]`
Do you really need the second urls page? You could just set your first urls.py as:
from django.conf.urls import url,include
from django.contrib import admin
from appone import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#home page
url(r'^$', views.index, name='index'),
url(r'^projects/', views.projs, name='proj'),
url(r'^formpage/', views.form_view, name='form_view'),
]
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 just started playing with Django, I love it! I followed the tutorial from the Django documentation, but have the following question:
I only have one app (polls), currently I always have localhost/polls/{urlname}
Is there a way to remove the polls keyword? So people that go to localhost automatically go to my app polls? At the moment, I have this wildcard
url(r'.*$', RedirectView.as_view(url='polls/', permanent=False), name='index'),
But this still keeps polls in the url. This is my complete urls.py file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'.*$', RedirectView.as_view(url='polls/', permanent=False), name='index'),
)
Thanks in advance!
Just remove the polls/ prefix in the regex of the "include" url:
urlpatterns = patterns('',
url(r'^', include('polls.urls', namespace="polls")),
...
)