Django Reverse function not working in this example - python

I'm not able to access the url named "forum" using the reverse function. It gives me this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'forum' not found. 'forum' is not a valid view function or pattern name.
I'm able to access all my other namespaces URLs within the other apps. But I just can't access anything by name from my root URLs.py file.
Here is my root URLs.py file
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls), name='forum'),
path('admin/', admin.site.urls),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Remove:
from machina import urls as machina_urls
Change the url to this:
path('forum/', include(machina.urls),),
in the urls.py file of machina add above the urlpatterns:
app_name = 'machina'
If you are going to use reverse remember:
reverse('app_name:url_name')
For example we have the following url in our machina.urls:
path('bitcoin/', views.bitcoin, name='bitcoin'),
Your reverse would look like this:
reverse('machina:bitcoin')
docs: https://docs.djangoproject.com/en/3.1/ref/urls/#include << Class based overview
Examples: https://docs.djangoproject.com/en/3.1/topics/http/urls/#namespaces-and-include << Copy and write overview
Other option is to have a separate file with url pattern lists for example:
url_list.py
from django.urls import path
from yesyes import views
varvar = [
path('', views.testbased, name='index'),
]
urls.py:
from django.urls import path, include
from yesyes.url_list import varvar as urllist
urlpatterns = [
path('admin/', admin.site.urls),
path('test/',include(urllist), name='test'),
]

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".

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

Django can't map url to view using include method

I have the following urls.py in my project dir,
Main project urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.render_calculator, name='render_calculator'),
path('calculator/', views.render_calculator, name='render_calculator'),
path('disclaimer/', views.render_disclaimer, name='render_disclaimer'),
path('cookiepolicy/', views.render_cookiepolicy, name='render_cookiepolicy'),
path('privacypolicy/', views.render_privacypolicy, name='render_privacypolicy'),
path('dashboard/', views.render_dashboard, name='render_dashboard'),
path('about/', views.render_about, name='render_about'),
path('admin/', admin.site.urls),
path(r'^', include('accounts.urls'))
]
Now I created a new app accounts (I added it to my apps in settings.py) where I would like to store the urls of that app in its own dir like so:
Accounts app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('register/', views.render_register, name='render_register'),
]
Accounts app views.py:
from django.shortcuts import render
def render_register(request, template="register.html"):
return render(request, template)
However, this configuration throws me this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/register
Using the URLconf defined in CFD.urls, Django tried these URL patterns, in this order:
[name='render_calculator']
calculator/ [name='render_calculator']
disclaimer/ [name='render_disclaimer']
cookiepolicy/ [name='render_cookiepolicy']
privacypolicy/ [name='render_privacypolicy']
dashboard/ [name='render_dashboard']
about/ [name='render_about']
admin/
^
The current path, register, didn't match any of these.
Where is the missing piece?
You are using path() with the regex r'^' which is causing your problem.
In order to define a path with a regex, you need to use re_path.
So change it to the following line:
re_path(r'^', include('accounts.urls'))
or you can use
path('', include('accounts.urls'))
Change this.
path(r'^', include('accounts.urls')) to
path('', include('accounts.urls'))

Url in template from site-package

i'm building my first Django app so i'm know i'm missing a lot of things, but i installed a gallery for my site and is inside site-packages, i set the url inside urls.py and it looks like this :
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I can perfectly access the gallery and its functionalities if i write the url in the browser, but i cannot reference the gallery Url inside my template like the other Urls using {% url 'gallery' %}, i keep getting this error :
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
Also heres the gallery url:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
Thanks!
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
Change it as shown above and call gallery:album_list for album list, and follow same pattern for others

Categories