django-registration activate url matching - regex

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

Related

Django url having %20 issues

One of my urls in url.py goes like this is like this
path('<str:subject>/<str:agglevel>/<str:cust_cd>/',views.Customer.as_view())
cust_cd takes value from UI. cust_cd is basically customer name which is a string value.
Url works fine for single words for cust_cd. Ex:Google,Gmail etc. But when I give words with spaces like Ex:You tube I get 404 error. It says you%20tube not found. Am not able to figure out how to configure url so that it accepts space characters.
in your urls.py file
instead of
<str:cust_cd>
try this
<slug:cust_cd>
for more information check out this link: https://docs.djangoproject.com/en/3.0/topics/http/urls/#path-converters

Django url pattern doesn't match

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.

Django URLconf: getting a 404 error

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

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

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