I have two files, one containing the URL like this
url(r'^users/', include('api_manager.users_urls', namespace='user'))
in second URL file i have this:
url(r'^', users_views.UsersList.as_view(), name='index')
When i hit the URL localhost:8000/api/users/ It redirect me to view 'UserList' and here i am using reverse function
reverse("user:index")
it returns me the /apiusers/ instead of /api/users/
Please let me know what is wrong in it.
I have resolved the issue, i find out that i am using api/* in my parent URL file, i just remove the '*' and reverse function returning the /api/users/
:)
Related
I have a urls.py file which contains multiple urls with same named paramaters and regex but different names. When I call a view function from the template using {% url 'name' param %} it calls the function which comes first in the urls.py file in disregard to the name. Here is the content of urls.py:
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_password',name='reset_password'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_securityquestions',name='reset_securityquestions'),
I am trying to call reset_securityquestions from the template using:
"{% url 'reset_securityquestions' account.uuid user.uuid %}">
but it calls reset_password instead.
If I change the order of urls in urls.py to this:
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$', 'reset_securityquestions',
name='reset_securityquestions'),
url(r'(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$', 'reset_password', name='reset_password'),
and call reset_password using:
{% url 'reset_password' account.uuid user.uuid %}
it calls the reset_security_questions function. Where am I going wrong?
Django always take firsts matching url pattern , so rewrite the urls as :
urlpatterns = patterns('accountsearch.views',
url(r'^$', 'account_search', name='account_search'),
url(r'^reset-password/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_password',name='reset_password'),
url(r'^security-question/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$','reset_securityquestions',name='reset_securityquestions'),
I don't understand how you are expecting Django to know which URL to use if they both have exactly the same url pattern. When the request comes in, all Django has to go on is the URL itself, and it will always try and match those in order. With your switched patterns, you'll find that the reset_password view now doesn't work.
You need to give Django some way of distinguishing between them. Usually some literal text, either as a prefix or a suffix, would be used, for example:
r'reset/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$'
r'security/(?P<account_uuid>[a-zA-Z0-9\-]+)/(?P<user_uuid>[a-zA-Z0-9\-]+)/$'
How can I support old and new URI versions both working without breaking the reverse()?
For example, I have:
urlpatterns = patterns('',
url(r'^(old_part|new_part)/other/$', 'some_view'),
)
In this case /old_part/other/ and /new_part/other/ point to the same view but reverse() method fails because it doesn't know how to form link properly.
Also, what if we have url(r'^(old_part|new_part)/other/', include(sub_patterns)) how can it be handled?
Do you have any ideas?
Thanks for your help.
I suppose you are migrating. This means you don't want old url work, you want it to redirect to new url. Probably with 301 HTTP code (permanent redirect).
Having several urls for same content makes your site harder to use and hurts your SEO. Permanent redirect will tell Google and any other search engine to reindex page with new address.
You can do it this way in Django:
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^new_part/other/$', 'some_view'),
url(r'^old_part/other/$',
RedirectView.as_view(url='new_part/other/', permanent=True)),
)
If you need to capture everything with a subpath, you can capture url ending and add it to redirect url this way:
urlpatterns = patterns('',
url(r'^new_part/other/$', include(sub_patterns)),
url(r'^old_part/other/(?P<rest>.*)$',
RedirectView.as_view(url='new_part/other/%(rest)s', permanent=True)),
)
You can use redirect_to generic view in Django 1.4 and earlier.
If you want without a redirect, you could try this
url(r'^(?P<url>old_part|new_part)/other/$', 'some_view', name='some_view'),
Then your view will look like this
def some_view(request, url):
...
Then call reverse like this:
# will return /old_part/other/
reverse('some_view', kwargs={'url': 'old_part'})
# will return /new_part/other/
reverse('some_view', kwargs={'url': 'new_part'})
Just redirect the old urls to the new ones (with a 301 Moved Permanently).
NB : if you really insist on supporting both sets of urls (not a good idea IMHO), you'll have to have two distinct url patterns, and choose which one reverse() should resolve to.
I am using django-registration app and I am implementing the Remember me functionality snippet
Basically, the registration app, needs you to define one URL only,
url(r'^accounts/', include('registration.backends.default.urls')),
under the hood, it defines default urls like /accounts/login, /accounts/logout, each of which points to django.contrib.auth.views functions.
I need to overwrite the login() function, as well as the accompanying URL.
So how do I overwrite URL accounts/login in my urls.py, keeping all the rest urls as default?
Django will use the first URL pattern that matches. So in your urls.py, add a pattern for accounts/login before you include the urls from django-registration. All other URLs will be handled by django-registration.
You could try explicitly catching the accounts/login url request before it hits your more general accounts/* url catcher.
Maybe
# first catch your custom login
url(r'^accounts/login', include('my_custom_login.urls')),
# and everything else beginning with accounts/
url(r'^accounts/', include('registration.backends.default.urls')),
I'm attempting to pass a string from the end of a url to a view function, but I'm getting the entire url passed to render_form() i.e. if I input "myurl.me/prepend/identifier" I get "prepend/identifier/" when I just want "identifier"
These are my urls.py files, the top one is located in an app directory, the bottom one is in the project dir:
urlpatterns = patterns('',
url(r'^(?P<identifier>.+)$', views.render_form),
)
--
urlpatterns = patterns('',
url(r'^prepend/', include('the_ones_above.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think it might have somehting to do with the split over 2 urlpatterns in 2 separate files, any ideas what im doing wrong?
Thanks!
edit: the view looks like: def render_form(request, identifier=None):
oh, and this is the latest django (from git)
2nd edit: removed the rather superfluous 1st url, behavior is still the same
I will suggest you to simply write you url like.
url(r'^(?P<identifier>[\w]+)/$', views.render_form),
[\w] will tell you that you can pass word character (A-Za-z0-9_).
I am very new to Python and Django.
I am using Django - 1.4.5. I am trying to get user input from form, redirect the url with the user input like
Httpresponseredirect('abc/xyz/%s' %variable)
I could see in browser that the response is redirected, but it throws a 404 error. The urls.py also has the url defined.
urls.py
urlpatterns = pattern('',
url (r'abc/xyz/(?P<variable>)/pqr' , 'view_name')
)
Can you please throw some light on it.
On further debugging I could figure out that the issue is with passing the argument to reverse function. When I pass a static page with no arguments, it redirects to the correct view. But when I redirect with arguments it throws me a NoreverseMatch error. I am redirecting it this way
return HttpResponseRedirect(reverse('view_name', kwargs= {'group':'group'}))
In urls.py it is defined as:
url (r'app/$' , 'app.view.app') ,
url (r'^my/first/(?P<group>)/$ , 'app.webapi.json.list_record', name ='view_name;),
list_record is a function in app.webapi.json.py.
Please let me know if any more details is required.
User reverse function in you view, first add a url name:
Httpresponseredirect(reverse('my_view_name', args=[variable]))
and (like #sushail said) fix the regexp:
urlpatterns = pattern('',
url (r'^abc/xyz/(?P<variable>)/pqr/$' , 'view_name', name='my_view_name')
)
you need to use complete path as the regexpression matchs abc/xyz/..../pqr
Httpresponseredirect('/abc/xyz/%s/pqr/' %variable)
and like (like #lalo said)
urlpatterns = pattern('',
url (r'^abc/xyz/(?P<variable>)/pqr/$' , 'view_name')
)
please checkwith / at begining and end, what is the view_name, it looks like you must have such a view in project directory,or you have to add the apppath in urlpatterns
`urlpatterns = pattern('<yourappname>.views',`