Django URLconf: getting a 404 error - django

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

Related

Django URL explanation

I'm facing an issue in Django URLs which is when I direct to localhost:8000/dashboard or localhost:8000/dashboard1111111111, it opens the same page.
Although in my urls.py file I have only /dashboard
How do I throw an error page if URL is incorrect?
Add $ at the end of the regular expression:
url(r'^dashboard$', views.dashboard, name='dashboard'),
This would require the end of the string after the dashboard word.

django-registration activate url matching

I'm implementing the django-registration package to my project. Everything worked smoothly but when you go to the link sent to the email with the activation key in the URL, django doesn't match any URL to the given URL.
The generated URL sent by email is
http://127.0.0.1:8000/accounts/activate/23c768c78ecd7af9b1516e37013901fd9ea=0b062/
and one of the URL that django tries to match it with is:
^accounts/ ^activate/(?P<activation_key>\w+)/$ [name='registration_activate']
but apparently it doesn't match. Any ideas what might be wrong?
My main urls.py contains
(r'^accounts/', include('registration.urls'))
and in registration.urls has
url(r'^activate/(?P<activation_key>\w+)/$',
activate,
name='registration_activate')
i don't think = is an alphanumeric character that \w would match you will have to add = to your regex.
also the carrot ^ denotes a the start of the field so
^accounts/activate/(?P<activation_key>\w+)/$ [name='registration_activate']
would work. Unless you are saying that ^accounts is in your main urls.py and ^activate is in an included urls.py in which case your question is a little confusing to me.
Found the solution on Equals sign in django-registration activation url
The problem was that my mail "client", in cmd was adding a '=' sign to concatenate lines.
Thanks

Regex Url conf Django

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),

Django "Page not found" error page shows only one of two expected 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.

Enable sluggified URLs in Django

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.