Django - Url patatterns dose not work - python

I started working with Django yesterday and I have a little error. I searched on the internet but I cant find the answer for my problem. I made the main app and then I make a little app and tried to link it to the main. Here is my code
urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('^$', include('personal.urls'));
]
in my personal.urls I have
from django.urls import path, include
from . import views
ulrpatterns = [
path('^$', views.index, name="index");
]
and in views.index I have
def index(request):
return render(request, 'personal/home.html')
Any help would be awesome, thank you!!!

You are using the wrong tool here: since django-2.0, there are basically two ways to specify a URL:
with a regex through re_path [Django-doc], url [Django-doc] is an "alias" of this, but will probably eventually get deprecated; and
by using a specific "path pattern" syntax with path [Django-doc]
Here you somehow mixed the two, and use path(..) function for a "regex-specified" URL.
Using path for path pattern-based URLs
We can also fix it by using path:
# urls.py
from django.contrib import admin
from django.urls import path, include
ulrpatterns = [
path('admin/', admin.site.urls),
path('', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import path, include
from . import views
ulrpatterns = [
path('', views.index, name="index");
]
Using re_path for regular expression-based URLs
We can also fix it by using re_path instead:
# urls.py
from django.contrib import admin
from django.urls import re_path, include
ulrpatterns = [
re_path('admin/', admin.site.urls),
re_path('^$', include('personal.urls'));
]
and:
# personal/urls.py
from django.urls import re_path, include
from . import views
ulrpatterns = [
re_path('^$', views.index, name="index");
]

Related

How do I import multiple views for urls.py?

I need to import views from horoscopes and dates, but when importing them into urls.py there is a conflict because of which only one views is perceived.
So my question is: how do I import multiple views?
I've tried several approaches, e.g.:
from django.contrib import admin
from django.urls import path
import horoscopes
import dates
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', horoscopes.views.monday),
path('dates/monday', dates.views.monday),
]
as well as this one:
from django.contrib import admin
from django.urls import path
from horoscopes import views as horoscopes_views
from dates import views as dates_views
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', views.leon),
path('dates/monday', views.monday),
]
But both options still ignore one of the views.
As voodoo-burger was suggesting, the browser can only display one page at the time. So either rename your URLs to more specific names in order to differentiate between the views, or you are looking for URL Namespacing.
From the Django Documentation:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
...
]
Then call your URL like this:
reverse('polls:index', current_app=self.request.resolver_match.namespace)
You can try following code
from django.contrib import admin
from django.urls import path
from horoscopes import views as horoscopes_views
from dates import views as dates_views
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/leon', horoscopes_views.leon),
path('dates/monday', horoscopes_views.monday),
]
I found the most optimal solution to my problem and it consisted in creating a separate file in each application urls.py , separately adding all URL addresses there in this form:
in each of my applications, the necessary horoscopes and I added a separate file called urls.py and I entered the following into it:
from django.urls, import the path
from . import views # the dot indicates that we are referring to the directive in which the file is located
urlpatterns = [
path('scorpion/', views.scorpion),
path('leon/', views.leon),
]
and according to the same scenario, I added to another application for the url
from django.utils, import the path
from . import views
urlpatterns = [
path('monday/', views.monday),
]
The main one includes the application urls.py it looks like this:
from django.contrib, import admin
from the django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('horoscopes/', include("horoscopes.urls")),
path('dates/', include("dates.urls")),
]

How does Django know to call the index?

I just started learning Django and I am a bit confused. I'm following the official documentation and this is the code:
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
polls/views.py
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from django.views import View
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I am mainly confused that if I add more functions in polls/views.py, how will using inclde('polls.urls') know that I wish to call the index function or some other function?
Link for the documentation page that I am following: https://docs.djangoproject.com/en/3.2/intro/tutorial01/
Essentially it constructs a list of paths (strings) that it tries to build up and then find which you wanted
so paths in polls for "" and foobar would lead django to try to match up to the following urls
polls/
polls/foobar
The full path for the views.index is 'polls/' + '' and thus 'polls/', so it appends the two.
If you make an extra path:
# polls/urls.py
urlpatterns = [
path('', views.index, name='index')
path('foo/', views.other_view, name='other-view')
]
then that path pattern will be 'polls/'+'foo/' = 'polls/foo/', so it will disambiguate between the two.
If you thus visit polls/, then the first path will match and thus "fire" index. If you visit polls/foo, then the other-view will "fire".

How do I fix the circular import in django

I am trying to simply put a 'Hello World' text on the server. And it brings an error.
Project Urls.py :
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
App Urls.py
from django.urls import path
from . import views
urlpattern = [
path('', views.index,name = 'index')
]
Views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(response):
return HttpResponse('<h1>Hello World</h1>')
The Error it tells me:
The included URLconf in myapp/urls.py does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
You are missing an 's' in your apps urls.py.
it has to be 'urlpatterns', instead of 'urlpattern'
urlpatterns = [
path('', views.index,name = 'index')
]

Error message in Django when I try to runserver - does not appear to have any patterns in it. If you s ee valid patterns in the

Every time I write python manage.py run server. I get the error message:
The included URLconf '<module 'strana.views' from 'D:\\novi projekat\\strana\\views.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
mysite urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.views')),
]
myapp(named strana)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Kita u Bila')
You included your views module. You want to include the module that contains your urls.
from django.contrib import admin from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('strana/', include('strana.urls')),
]

Cannot open another HTML file from Django

I was trying to call one html file from another via Django.
I actually have no errors inside my project(It works). But when I am trying to call the second html file the page refreshes only. Here is my code, thanks:D. My projects name is information and the name of my app is basic.
basic.views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,'basic/index.html')
def customer(request):
return render(request,'basic/customer.html')
basic.urls.py:
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('',views.index,name='index'),
path('',views.customer,name='customer'),
]
information.urls.py:
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('',views.index,name="index"),
path('admin/', admin.site.urls),
path('',include('basic.urls'))
]
The problem is that you have two paths that match exactly the same path. So that means each time you make a request with an empty path, the first one is used.
You should make use of non-overlapping paths, for example:
# basic/urls.py
from django.urls import path
from basic import views
app_name='basic'
urlpatterns=[
path('', views.index, name='index'),
path('customer/', views.customer, name='customer'),
]
and in your information/urls.py:
# information/urls.py
from django.contrib import admin
from django.urls import path,include
from basic import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('basic/', include('basic.urls'))
]

Categories