Root url file is not communicating with app url file - python

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

Related

Error in Django: Using the URLconf defined in django_test.urls, Django-3 tried these URL patterns, in this order:

I've tried setting these URL's in this project, but I get a 404 when trying to access the registration.html file and the api-auth URL that is related to a REST API
For reference:
index = app
django_test = site
index/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', views.homepage, name="homepage"),
path('/register', views.registration, name="registration"),
path('api', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
]
django_test/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')),
path('admin/', admin.site.urls)
]
index/views.py
from django.shortcuts import render
def homepage(request):
return render(request,"home/homepage.html")
def registration(request):
return render(request, 'home/registration.html')
All of our HTML files are located in index/templates/home directory
Can you try changing name="registration" to name="register" ?
Change the URL "register/" to "/registration"

I want to load another view file after the url / but receiving an error saying page not found

Error message:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/music/2/
Using the URLconf defined in Analytic_practice.urls, Django tried these URL patterns, in this order:
admin/
music/ [name='index']
music/ (?P<album_id>[0-9]+)/ [name='detail']
The current path, music/2/, didn't match any of these.
Here is my code:
music.urls.py file:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('(?P<album_id>[0-9]+)/', views.detail, name='detail'),
]
views.py:
from django.http import HttpResponse
# noinspection PyUnusedLocal
def index(request):
return HttpResponse("<h1>This will be a list of all Albums</h1>")
# noinspection PyUnusedLocal
def detail(request, album_id):
return HttpResponse("<h2>Details for Album id: " + str(album_id) + "</h2>")
Analytic_practice.urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('music/', include('music.urls')),
]
You are messing path with re_path. As, id only contains integer. So:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:album_id>/', views.detail, name='detail'),
]
Refs

Page not found Error in my Django Project

I just started learning Django and got the following error:
[![The site doesn't see URL and then Error 404][1]][1]
urls.py - site
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('webexample/', include ('webexample.urls')),
]
urls.py - webexample
from django.urls import path
from . import views
urlpatterns = [
path(r'^$', views.index, name='index'),
]
views.py - webexample
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<h3>This page isn't set up yet...</h3>")
Error photo
change path to this to work for the current url.
path(r'', views.index, name='index')
You could do this
path('index/', views.index, name='index'),
or
re_path(r'^index/$', views.index, name='index'),
to know more about this, you can the django doc https://docs.djangoproject.com/en/3.0/ref/urls/

Django URL routing for one app

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

Page not found with Django (404)

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

Categories