Path is not match any of these - django

I always get this error "The empty path didn't match any of these." When I try to access the page through this url:
url('^about/$',views.AboutView.as_view(),name = 'about')
and when I remove "^about/$" part, then it works:
url('',views.AboutView.as_view(),name = 'about')
How could I resolve it?
This is link for call:
<li><a class="navbar-brand" href="{% url 'about'%}">About</a></li>
this is view.py
class AboutView(TemplateView):
template_name = 'about.html'
and, this urlpatterns
urlpatterns = [
url('^about/$',views.AboutView.as_view(),name = 'about')
]

from django.conf.urls import url
from blog import views
urlpatterns = [ url('about',views.AboutView.as_view(),name='about') ]
instead of this
from django.urls import path
from blog import views
urlpatterns = [
path('about/', views.AboutView.as_view(),name='about'),
use this pattern same as your main url

path('about/', views.AboutView, name='about'),

It's not good to follow 2 ways of creating urls, Since django==2.0 they have introduced very nice and easy way to declare urls.
In the old way...
from django.conf.urls import url
urlpatterns = [
url(r'^about/$', AboutView.as_view(), name="about")
]
But In the new way it's lot more cleaner...
from django.urls import path
urlpatterns = [
path('about/', view.AboutView.as_view())
]
But if you want to stick with the regular expressions, Use re_path() instead of path().
urlpatterns = [
re_path(r'about/$', view.AboutView.as_view())
]
In my it's better stay with one pattern, old or new but not both. It makes your code look more cleaner.

Related

Django set default empty url

I'm trying to learn Django and I'm following Corey Shafer's tutorials (https://www.youtube.com/watch?v=a48xeeo5Vnk), but when I try to make two different pages, I get automatically directed to the one with an "empty address":
In his:
/Blog
/urls.py
it looks like this:
from django.conf.urls import path
from . import views
urlpatterns = [
path('', views.home, name='blog-home'),
path('about/', views.about, name='blog-about'),
]
and when he goes to localhost:8000/blog/about, the page displays correctly
When I try to imitate his code for blog/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
the result of the localhost:8000/blog/about is the content of views.home, and not views.about.
The following works correctly, when I write a name instead of an empty string:
urlpatterns = [
url(r'^home', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
But I don't understand why it worked in a previous version, why it won't work now, and what could fix it
A url matches if it can find a substring that matches, the empty string r'' thus matches every string.
You should make use of anchors to specify the start (^) and end ($) of the string:
urlpatterns = [
# &downarrow; &downarrow; anchors
url(r'^/$', views.home, name='blog-home'),
url(r'^about/', views.about, name='blog-about'),
]
Note: As of django-3.1, url(…) [Django-doc] is
deprecated in favor of re_path(…) [Django-doc].
Furthermore a new syntax for paths has been introduced with path converters: you
use path(…) [Django-doc] for that.

Building URL from name

I've got a url of 'accounts:produce_setup' (i.e. namespace/app is 'accounts' and name of url is 'product_setup'). I'd like to build the full url associated with this view and pass it as context into a template.
How would I go about doing this? Would I use build_absolute_uri()?
Thanks!
you should include your app in project_name/urls.py and bind your application to special URL pattern like this:
from account.urls import urlpatterns as account_urlpatterns
urlpatterns = [
url(r'^account/', include(account_urlpatterns, namespace='account')),
url(r'^admin/', admin.site.urls),
]
and after this in your account/urls.py you can implement your urlpatterns and set your special name for each url like this:
from django.conf.urls import url
from .views import produce_setup_view
urlpatterns = [
url(r'^produce_setup/$', produce_setup_view, name='produce_setup')),
]
at the end now you can use them in your template and views or any python file in your django project like this:
.py in django project:
from django.urls import reverse
url_string = reverse('account:produce_setup')
print(url_string)
>>> '/account/produce_setup/'
in template:
Good Luck :)
Actually, you don't need to. You can simply use {% url "accounts:product_setup" %} in template. For more details check here. And if you want to build the url is views(maybe for other reasons) you can use reverse.

Support for string view arguments to url() is deprecated and will be removed in Django 1.10 (got about). Pass the callable instead

From django.conf.urls import url
urlpatterns = [
'chat.views',
url(r'^$', 'about'),
url(r'^new/$', 'new_room'),
url(r'^(?P<label>[\w-]{,50})/$', 'chat_room'),
]
can any one please help me
try below code
from chat import views
from django.conf.urls import url
urlpatterns = [
url(r'^$', views.about, name='about'),
url(r'^new/$', views.new_room, name='new_room'),
url(r'^(?P<label>[\w-]{,50})/$', views.chat_room, name='chat_room'),
]
You should not use strings in the view arguments. Try this instead:
from chat.views import about, new_room, chat_room
from django.conf.urls import url
urlpatterns = [
url(r'^$', about),
url(r'^new/$', new_room),
url(r'^(?P<label>[\w-]{,50})/$', chat_room),
]

How to register DRF router url patterns in django 2

My DRF routers specify a namespace so that I can reverse my urls:
urls.py:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include(router.urls, namespace='widget-api'),
]
Which, when upgrading to django 2, gives:
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.
Django 2 now requires app_name if the namespace kwarg is specified when using include. What's the right way to specify app_name when the url patterns are constructed by a DRF url router? I don't think the documentation is up-to-date for django 2 on this subject.
You need to put app_name = 'x' in your application's url.py file. This is a little buried in the documentation:
https://docs.djangoproject.com/en/2.0/topics/http/urls/#id5
For example, if in /project/project/urls.py you have:
path('', include('app.urls', namespace='app'))
Then in the corresponding url file (in /project/app/urls.py) you need to specify the app_name parameter with:
app_name = 'app' #the weird code
urlpatterns = [
path('', views.index, name = 'index'), #this can be anything
]
It's just necessary to use '{basename}-list' in reverse function.
In your case, it's going to be: reverse('widgets-list')
You need to include the router.urls as a tuple and add the app name to the tuple instead of only include router.urls
According to your example you should try with something like:
router = DefaultRouter()
router.register('widget/', MyWidgetViewSet, base_name='widgets')
urlpatterns =+ [
url(r'/path/to/API/', include((router.urls, 'my_app_name'), namespace='widget-api'),
]
The recommended approach is
from django.conf.urls import url, include
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'widget/', MyWidgetViewSet)
urlpatterns = [
url(r'^path/to/API/', include('rest_framework.urls', namespace='widget-api'))
]
See http://www.tomchristie.com/rest-framework-2-docs/tutorial/quickstart#urls

What's wrong with my urls.py?

I'm trying Django 1.11 and I have an issue :
This my file agora/agora/urls.py :
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^start/', include('start.urls', namespace='start')),
]
And this is my file agora/start/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', view_first, name='view_first'),
url(r'^/inscription', view_second, name='view_second'),
url(r'^/about', view_about, name='view_about'),
]
The Django server return an error :
NameError: name 'view_first' is not defined
Many thanks for your answers !
You should do like this for all functions
views.view_first
views.view_second
....
you have to specify the function name in the import statement
from django.conf.urls import url
from .views import view_first, view_second, view_about
urlpatterns = [
url(r'^$', view_first, name='view_first'),
url(r'^/inscription', view_second, name='view_second'),
url(r'^/about', view_about, name='view_about'),
]
or import the view and assign each function with view.
from django.conf.urls import url
from . import view
urlpatterns = [
url(r'^$', view.view_first, name='view_first'),
url(r'^/inscription', view.view_second, name='view_second'),
url(r'^/about', view.view_about, name='view_about'),
]
Although, the question has already been answered, I did happen to notice a minor but mistake.
When writing urls, adding "$" sign at the end of the endpoint is always recommended. Not adding the same may cause overlapping of similar urls which could confuse the app behaviour and even can cause some serious errors.
The dollar sign marks the end of the url string.
Just a heads up!!