Django url pattern doesn't match - regex

I am using new Django 1.8 app to learn Django.
I am stumped as to how to get this my simple url to be resolved by urls.py
I create the url in another view as:
<a href="/photoview/{{photo.id}}/"}>
I can successfully pass this url to the browser as:
http://localhost:8000/photoview/300/
I am expecting that this url can be matched by the urls.py expression:
url('r^photoview/(?P<id>\d+)/$', views.photoview),
But this is not working. I have tried variations of this but none have worked so far, such as:
url('r^photoview/(?P<id>[0-9]+)/$', views.photoview),
I get this message in browser when it fails to match
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/photoview/300/
Using the URLconf defined in asset.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='index']
^time/$
^about/$
^accounts/$
^photos/$
^tags/$
^users/$
r^photoview/(?P<id>\d+)/$
^static\/photos\/(?P<path>.*)$
The current URL, photoview/300/, didn't match any of these.
Appreciate any help getting this to work.

you have url('r^photoview/(?P<id>\d+)/$', views.photoview),
you want url(r'^photoview/(?P<id>\d+)/$', views.photoview),
(Note the r is in front of the string, not the first character)
As noted in docs.djangoproject.com/en/1.8/topics/http/urls,
The 'r' in front of each regular expression string is optional but
recommended. It tells Python that a string is “raw” – that nothing in
the string should be escaped
Also note that you should use a friendly name in your url definition (e.g. photoview) and then use {% url 'photoview' photo.id %} in your template instead of hardcoding the URL pattern.

Related

save button not working in django

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/post/new/blog/home_page.html
Using the URLconf defined in school.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home_page']
^about/$ [name='about']
^event/$ [name='event']
^calendar/$ [name='calendar']
^contact/$ [name='contact']
^class_a/$ [name='class_a']
^login/$ [name='login']
^login_submit/$ [name='login_submit']
^class_info/$ [name='class_info']
^Ist_std/$ [name='Ist_std']
^IInd_std/$ [name='IInd_std']
^IIIrd_std/$ [name='IIIrd_std']
^IVth_std/$ [name='IVth_std']
^post/new/$ [name='post_new']
The current URL, post/new/blog/home_page.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Your URL is not matching any of your defined URLs. If you want that page to show anything, you'll need to include a match like:
r'^/post/new/blog/home_page$'
In url.py and write a view for it to display the wanted html file.

404 on a simple template view - strange

I have a django app set up consisting of ListViews, TemplateViews etc..
So, I just added a small templateview to it like so:
#views.py
class TermsTemplateView(TemplateView):
template_name = "terms.html"
#urls.py
url(r'^terms/$', TermsTemplateView.as_view(), name='terms'),
and in terms.html, I am using for linking:
Terms & Conditions
For some strange reason, I keep getting 404 on localhost/terms as follows:
404: No <model_name> found matching the query
I am baffled why this is happening all of a sudden. I have the same set up for "about", "thanks", "contact" pages, and they seem to display it with no problems.
..and the worst part is, if I modify the urls.py like so:
url(r'^/terms/$', TermsTemplateView.as_view(), name='terms'),
and then go to http://127.0.0.1:8000//terms/ - the page seems to be there.. I am surprised why this is so :(
Any help would enlighten me!
The / at the end is the culprit of your problems. localhost/terms doesn't match '^terms/$' regular expression, localhost/terms/ does.
You can make / at the end optional by using ?:
url(r'^terms/?$', TermsTemplateView.as_view(), name='terms'),
UPD: Note that there is a better solution to the problem, APPEND_SLASH:
When set to True, if the request URL does not match any of the
patterns in the URLconf and it doesn’t end in a slash, an HTTP
redirect is issued to the same URL with a slash appended.
Also see:
Why would you need a slash at the end of a URL?
django - url with automatic slash adding
Append Slashes to URLs in Django

Url error in django social auth

I am using django social auth in my app and I found a Page not found error.
I have installed it in my app and also place it in my project parallel to my app.
And I have also add url to my url file
url(r'', include('social_auth.urls')),
But when it shows me the error It shows the list of url in my url file and where i have the required url which i am looking for.
The error page is as :
Page not found (404)
Request Method: GET
Request URL: http://abc.com/login/
Using the URLconf defined in webdev.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^polls/
^admin/doc/
^admin/
^register1/
^edit/
^edit1/
^customerreg/
^checkout/
^checkout1/
^order/
^order1/
^upload/
^upload1/
^product/
^profile/
^logout/
^login/(?P<backend>[^/]+)/$ [name='socialauth_begin']
^complete/(?P<backend>[^/]+)/$ [name='socialauth_complete']
^associate/(?P<backend>[^/]+)/$ [name='socialauth_associate_begin']
^associate/complete/(?P<backend>[^/]+)/$ [name='socialauth_associate_complete']
^disconnect/(?P<backend>[^/]+)/$ [name='socialauth_disconnect']
^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+)/$ [name='socialauth_disconnect_individual']
The current URL, login/, didn't match any of these.
In the above list you can see that url file have that url , but it doesn't access that ??
^login/(?P<backend>[^/]+)/$ [name='socialauth_begin']
This url do not match with http://abc.com/login/ as the url pattern requires a backend parameter.
Something like this will match the url you are accessing, however there is no backend parameter there, so in view you may want to choose something default.
url(r'^login/$', 'your_view',),

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