Django matching a url with paramater to start - django

I've got a site I'm working on where I need to match a parameter at the start
of the url:
test.com/the-data-slug
What is odd is I can't figure out why the following code dosen't work.
It dosen't match at all.
path('<slug:data-slug>/', DataLandingView.as_view(), name="data_landing")
I did get the following to work, but I know path is prefered over re_path.
re_path(r'^(?P<data-slug>\w+)/$', DataLandingView.as_view(), name="data_landing")
I'm really curious what I'm doing wrong with the path call, or is this just something path can't do?
Update:
Requested from commenters.
From urls.py these are the only url patterns in use.
urlpatterns = [
# Special Project level routing. Specifically routes to handle login/logout admin
path('admin/', admin.site.urls),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
# We are handling all convention level routing here as it makes more
# sence here as a master control router than in the individual projects
# Global Stuff for buisness
path('', HomePageView.as_view(), name='home'),
path('about/', AboutPageView.as_view(), name='about'),
# If we don't recoginze from the above we step down to the next layer
path('<slug:data-slug>', CLandingView.as_view(), name="c_landing")
]
Going to http://127.0.0.1:8000/abc-2020
and http://127.0.0.1:8000/abc-2020/
returns a 'Page Not Found' Exception
But if I use the following route with re_path. It works.
re_path(r'^(?P<c_name>[-\w]+)/$', CLandingView.as_view(), name="c_landing")

Related

/ in urlpatterns django

I am a beginner in django and don't understand the meaning "/" in urlpatterns. Here is the urlpatterns in my project urls. My first app's name is first.
urlpatterns = [
path('admin/', admin.site.urls),
path('first', include("first.urls"))
]
I found that it didn't work unless I amended it to following:
urlpatterns = [
path('admin/', admin.site.urls),
path('first/', include("first.urls"))
]
I don't understand what the meaning of '/' is. I googled but did not find an answer. Could anyone help me on this?
Another question is that the double quotes and single quote could be used either way. But is there any convention or better practice that I can follow to use single or double quote? Thanks a lot.
Short answer: it is used as a separator for the first and for the URL paths in your first app.
You are including the urlpatterns of the first app. Imagine that this app has as URL:
# first/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('foo/', foo)
]
Without the slash, the path to trigger foo is /firstfoo/ so it appends the two.
If you work with a slash, the path to trigger foo is /first/foo.

404 error issue with Path() pattern

path('add', views.TopicCreate.as_view(), name='add-topic')
]
When i try to run it (by entering http://127.0.0.1:8000/home/add), it gives me 404 error saying it didn't match any of the patterns. even though it says that there's matching url pattern right there.
home/ [name='index']
home/ <int:topic_id> [name='details']
home/ add [name='topic-add']
admin/
The current path, home/add/, didn't match any of these.
The weird thing is, the link works when i try it in Mozilla and Google Incognito. Doesn't work on IE (private window or not) and normal Chrome.
full urls.py :
app_name = "home"
urlpatterns = [
path('', views.index, name="index"),
path('<int:topic_id>/', views.detail, name='detail'),
path('add', views.TopicCreate.as_view(), name='topic-add')
]
I think you have APPEND_SLASH set to False in your settings or you don't have CommonMiddleware in your middlewares.
The reason behind the ERROR 404 is that your url does not end in a slash home/add but you are visiting the path with a slash home/add/. This will not raise an HTTP_ERROR if you have included CommomMiddleware, or set APPEND_SLASH to True in your settings.
Bear in mind that this will have some effects like it will issue a redirect, etc. Please see the documentation in the link above for more information.
If you don't want to set APPEND_SLASH, you can update your urls and add a slash at the end:
path('add/', views.TopicCreate.as_view(), name='add-topic')

Django 2: path('^$', home, name='home') not working

I am new to Django and trying to make a project, but I am facing a simple problem. I am writing a path in Django 2 for root and it's not working, but for other things it works. Can anyone point out why it's not working.
What is working:
path(r'home/', home, name='home'),
This is not working:
path(r'^$', home, name='home'),
And just to be clear: I am not loading both the line together. I comment one line at a time, so no order issues.
Beginning with Django 2 a new way of defining URL routes is introduced. There are the functions path and re_path.
You seem to mix some things. Here is how it should look like when using path:
from django.urls import path
urlpatterns = [
path('', home, name='home'),
# or
path('home/', home, name='home'),
]
From this example you can obviously use only one path. I gave them both for illustrating.
As you notice the paths aren't raw strings and don't contain regular expressions like ^ or $.
For using regular expressions use re_path.

Django: Changing the index site? (home page)

The index site on my Django homepage stopped working because of a problem that will take a very long time to fix. The site can't be down for that long so I am trying to change the index site so that if you go to the primary url you will atleast end up on the website.
What I have done is change the urls.py file in the primary application, where I simply replaced the line
url(r'^', include('news.urls', namespace='news')),
to
url(r'^', include('events.urls', namespace='events')),
in the urlpatterns list, where news is the faulty page and events is the page that I want to be shown. However, after pushing this to live nothing changed, and for some reason my local Django development server is not working.
Did I do anything wrong, or is there anything else I have to do as well?
Thanks.
In the events app make sure you have a URL such as
......
url(r'^$', views.EventsIndex.as_view(), name='index'),
......
Replacing news.urls with events.urls may cause problems, if there are views/templates that try to reverse news urls. It would be better to leave the include as it is, and add a new URL pattern above that for the index:
from events.views import home
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^', include('news.urls', namespace='news')),
...
]

How to expand error in Django dev server?

I have trouble figuring out what trailing slash that causing an error in Django. This is the error:
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'.
Remove this slash as it is unnecessary.
My urls.py looks like this:
urlpatterns += [
#url('^$', direct_to_template, {'template': 'index.html'}, name='home'),
url('^$', mezzanine.blog.views.blog_post_list, name='home'),
url('^', include('main.urls')),
url('^', include('mezzanine.urls')),
]
And in my main.urls it doesn't exist any '/' before. How can I troubleshoot where the problem exists? Can I expand the error when I run the dev server with manage.py?
Edit:
This is my main/urls.py:
from django.conf.urls import patterns, include, url
from . import views
app_name = 'main'
urlpatterns = [
# Create Blog
url(r'^create-blog/$', views.create_blog, name='create_blog'),
# Lists
url(r'^blogs/$', views.blog_list, name='blog_list'),
url(r'^blogs/new/$', views.blogs_new, name='blogs_new'),
url(r'^top/$', views.top_lists, name='top_lists'),
url(r'^latest/$', views.latest_posts, name='latest_posts'),
# Tags
url(r'^tags/(?P<tag>[\w-]+)/$', views.tag, name='tag'),
url(r'^tags/$', views.tags, name='tags'),
# Avatars
url(r'^avatar/', include('avatar.urls')),
# Various
url(r'^welcome/$', views.welcome, name='welcome'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^contact-us/$', views.contact, name='contact'),
]
Looking over the question again, perhaps you have literally defined a pattern as ^/ somewhere.
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
This system check by django is printing out the pattern that is causing problems. I made a quick django example, and when I put in a url such as...
urlpatterns = [
url('^test1/$', views.test1, name='test1'),
url('^/test2/$', views.test2, name='test2'),
]
?: (urls.W002) Your URL pattern '^/test2/$' [name='test2'] has a regex beginning with a '/'. Remove this slash as it is unnecessary.
As you can see it is giving me my URL pattern exactly as it is defined, ^/test2/$. I think your problem is likely in some other part of your urls.py file or one of the other urls.py files, most likely an include because you do not have the trailing $ in the pattern nor a name for the view.
Try searching through your project for a url defined as '^/' is my recommendation.