I am trying to enable sluggified URLs in Django of the form that SO uses: example.com/id/slug.
I have no problem enabling slugs, and have URLs currently set up of the form: http://127.0.0.1:8000/articles/id/ (eg. /articles/1/) and that works fine. The corresponding URLpattern is:
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
If I change the URL pattern to:
(r'^(?P<slug>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
Then I recieve the following eror when I navigate to http://127.0.0.1:8000/articles/another-article/:
The current URL, articles/another-article/, didn't match any of these.
If, however, I try:
http://127.0.0.1:8000/articles/1/
I get the error:
No article found matching the query
Ultimately I want to be able to navigate to an aricle via either:
http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/
I should have been just a little more patient before asking this question because I figured out the answer:
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
(r'^(?P<object_id>\d+)/(?P<slug>[-\w]+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
The first pattern allows URLs of the form /articles/1/ which means that the second urlpattern (to include the slug) is optional.
Related
from django.conf.urls import url
from .import views
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About/', views.About,name='About'),
url(r'^checkout/', views.checkout,name='checkout'),
url(r'^contact', views.contact,name='contact'),
url(r'^faqs', views.faqs,name='faqs'),
url(r'^help', views.help,name='help'),
url(r'^icons', views.icons,name='icons'),
url(r'^payment', views.payment,name='payment'),
url(r'^privacy', views.privacy,name='privacy'),
]
The error message:
Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/About.html
Using the URLconf defined in shop.urls, Django tried these URL patterns,
in this order:
admin/
^$ [name='index']
^about/$ [name='about']
^checkout/$ [name='checkout']
^contact/$ [name='contact']
^static\/(?P<path>.*)$
The current path, About.html, didn't match any of these.
This kind of an error could occur from 2 or 3 different scenarios.
In your case, you seem to put the wrong URL in the browser address bar.
Your correct URL should be http://127.0.0.1:8000/About (as you've written in the URL patterns).
Remember, About.html - is the HTML template you create inside the templates folder. Even though you route to the html page (with a string like: app_name/About.html) - the actual URL in the address bar will be according to what you write in the regex path r'^url_name'. If you write r'^About.html' in url patterns, then http://127.0.0.1:8000/About.html should work perfectly.
The second scenario (based on my experience) which could produce this type of an error is when you forget to pass the 'request' argument inside the method that defines view of the URL - in the respective views.py file.
You should be having a method named About which would look like this in views.py
def About(request):
return render(request,'app_name/About.html')
If you forget to pass argument in the paranthesis of About, this kind of an error could occur.
Finally, if you are using django 2, please start using re_path method to serve regex url patterns. The url method is likely to be depracated in future release.
Refer re_path documentation.
your URL will not be http://127.0.0.1:8000/About.html it will just be http://127.0.0.1:8000/about (remember urls are case insensitive), this will take you to your view which is named About, in your view you should reference your template in its render (about.html)
have you read the my first Django app https://docs.djangoproject.com/en/2.0/intro/tutorial01/ its a great place to start if you are unfamiliar with how django operates
What you are trying to hit is not a valid url, you have to hit http://127.0.0.1:8000/About as written in urls.py.
You have to understand the difference between urls and html templates, this About.html would be used in views while rendering like:
return render(request, 'your_app/about.html')
And for sure you can write a url if you want like this:
urlpatterns = [
url(r'^$', views.index,name='index'),
url(r'^About.html/', views.About,name='About'),
.
.
]
Check the documentation
The url which you provide in url ( ) method doesn't contain any suffix of .html
You can goto the about page directly by /About
urls.py in project root
url(r'^people/',include('profiles.urls'))
profiles/urls.py
urlpatterns = patterns('profiles.views',
url(r'^me/$','home',name='profile_home'),
url(r'^(?P<user_name>[^/])/$','public_profile',name='user_public_profile'),
url(r'^signup/$','register_user',name='signup')
)
I am getting a 404 error and Django isn't able to find the Url's when I type for existing users, such as /people/alice while the other two patterns for the profile home and signup are working fine. Infact, this was working fine yesterday but not now, and I can't locate the problem.
The error message:
Django tried these URL patterns, in this order:
....
....
^people/ ^me/$ [name='profile_home']
^people/ ^(?P<user_name>[^/])/$ [name='user_public_profile']
^people/ ^signup/$ [name='signup']
Your regex only allows for a single character to match the username. You need to add a +:
r'^(?P<user_name>[^/]+)/$'
Regex pattern in the url is not correct
url(r'^(?P<user_name>[^/])/$','public_profile',name='user_public_profile'),
Change it to use [\w]+
url(r'^(?P<user_name>[\w]+)/$','public_profile',name='user_public_profile'),
I'm trying to create a filtered FAQ page in Django. It filters by three categories, and defaults to 'all' for all three when someone hits the root URL. From urls.py:
keywords = ('key1','key2','key3')
searchurl = r'^category1/(?P<%s>\w{1,50})/category2/(?P<%s>\w{1,50})/category3/(?P<%s>\w{1,50})/$' % keywords
searchall = dict(zip(keywords,['all']*len(keywords)))
urlpatterns = patterns('my.path.views',
url(searchurl, 'faq', name='search_view'),
)
urlpatterns += patterns('django.views.generic.simple',
url(r'^$', 'redirect_to', {'url': searchurl, 'kwargs': searchall}, name='default_search'),
)
This has all been working fine in my testing in Safari. However, when I tried it in Firefox, navigating to the root URL returned a Page Not Found error. It had re-directed to "root/^category1/(/", as if the regular expression had been passed as a URL and everything after the first ? was interpreted as a query string. Any idea what might be causing this?
Thanks!
In your url pattern default_search, searchurl should be a url string, not a regular expression.
Looking at the Django docs on redirect_to, it looks like you can use string substitution from parameters captured from the url. You cannot substitute the searchall kwargs into the regex as you are trying. The following should work:
searchallurl = 'category1/all/category2/all/category3/all/'
url(r'^$', 'redirect_to', {'url': searchallurl,}, name='default_search'),
However if I understand your url config correctly, you don't need to redirect from the root url. Insead, call your faq view, with searchall as the optional dictionary:
url(r'^$', 'faq', searchall, name='default_search')
I am trying to get the following setup up going.
Flatpages: Where all my static sites are (like: about, contact,..)
Dynamic Pages:
Here I am trying to link from one of the Flatpages to a start site:
the regex in the url conf of this startsite I tried was:
(r'^myapp/start/(\d+)/$', 'mysite.views.def_that_should_just_show_hello_world'),
In the views I had:
def def_that_should_just_show_hello_world(request):
return HttpResponse("Hello experiment world")
If I go to
/myapp/ I get 404: No FlatPage matches the given query.
/myapp/start/ I get 404: No FlatPage matches the given query.
/myapp/start/1 I get
Exception Type: TypeError
def_that_should_just_show_hello_world takes exactly 1 argument (2 given)
I thought with this setup I would get "Hello experiment world" on EVERY page.
Where did I go wrong?
I dont understand the multiple sites approach in regexs.
What would I have to do to print hello world on all these sites?
And then, what would I have to do to display 1 image on all of these sites?
Thanks a lot for the help!
You regular expression has a matching group in it - the (\d+) bit.
This requires one or more numeric characters to appear at the end of the url for that view. If you do not include the number at the end, this regular expression will not match the url. (url matching works like any other regular expression matching).
When you do include the number, eg. /myapp/start/1 you then have another problem. Because there is a matching group, the part of the url in the brackets will be passed as another argument to your view. Views are always passed the request as their first parameter but in this case the '1' matched by the (\d+) is provided as a second argument. This is why you are hetting the TypeError in this case.
Django's documentation has a lot of information on how url dispatching works, read that through and see if that makes sense!
from your_app_name import views
from django.conf.urls import url
urlpatterns = [
url(r'^$',views.method_name,name ='index'),
path('admin/', admin.site.urls),
I'm working with Django, admittedly for the first time doing anything real.
The URL config looks like the following:
urlpatterns = patterns('my_site.core_prototype.views',
(r'^newpost/$', 'newPost'),
(r'^$', 'NewPostAndDisplayList'), # capture nothing...
#more here... - perhaps the perma-links?
)
This is in an app's url.py which is loaded from the project's url.py via:
urlpatterns = patterns('',
# only app for now.
(r'^$', include('my_site.core_prototype.urls')),
)
The problem is, when I receive a 404 attempting to utilize newpost, the error page only shows the ^$ -- it seems to ignore the newpost pattern...
I'm sure the solution is probably stupid-simple but right now I'm missing it. Can someone help get me on the right track...
Your pattern for the include will only match an empty URL string, change it to a prefix which should be mapped to the included urls, or remove the $ from that pattern.