I'm not sure if it's very specific but I think what I am going to ask is being used in various places in different contexts. My problem is mainly related to URL patterns that I've created for my Django application. I need a profile URL for all application users, so I am creating a URL pattern as below.
urlpatterns = patterns('apps.user_profile.views',
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
url(r"^app/$", 'app', name="app_page"),
)
As is very clear, I am mapping URLs with user names in the path to fetch user data dynamically. This is working fine, but the problem comes when the system gets a request for app page. In this case, the request goes to user profile, since it accepts all kinds of words and is ordered before app view in urls.py.
Question :
Is there any way to specify, where a request is not resolved in user_profile view, to continue looking at other URLs in the urls.py of app?
Patterns are matched in the order they are written, so simply moving your app pattern before your username pattern will solve your problem:
urlpatterns = patterns('apps.user_profile.views',
url(r"^app/$", 'app', name="app_page"),
url(r"^(?P<username>\w+)/$", 'user_profile', name="user_profile_page"),
)
Now, only if a url doesn't match app/ it will be sent to your user profile view.
Of course, you will have an issue the day a user signs up with the username of app.
Related
Django re_path did not match and I don't know the reason why.
urls.py
urlpatterns = [
..
re_path(r'^localDeliveryPayment\?paymentId\=(?P<UUID>[0-9-a-z]{32})$', verifyMobilePayReceived.as_view()),
re_path(r'^localDeliveryPayment$', Payment.as_view(), name = 'localDeliveryPayment'),
..
]
If url
www.example.com/localDeliveryPayment the user is directed to Payment view.
If url www.example.com/localDeliveryPayment?paymentId=00340000610febab0891e9008816d3e9 the user should be directed to verifyMobilePayReceived view. The problem is that right now www.example.com/localDeliveryPayment?paymentId=00340000610febab0891e9008816d3e9 is still directed to Payment view.
You are trying to capture a GET parameter in your URL routing which is the incorrect way of doing what you are trying to do.
Either continue with your current method of passing a paymentId GET parameter and check for its presence in you Payment.as_view() view OR you can rework this to something along the lines of.
re_path(r'^localDeliveryPayment/<UUID:NAME_OF_UUID_IN_MODEL>/', verifyMobilePayReceived.as_view()),
This should provide the filtering for the model you desire.
I have almost completed adding a PIN number field for users in the Django admin.
previous question
I have a separate form to change the PIN number, very similar to that seen when changing a password. I have an issue with the successful redirect after a form submit. I would like to redirect back to the user change form, but have been able to do so.
What is the url pattern required to redirect to that page on a successful redirect? When submitting a new password successfully you can redirect back to the user change page therefore it is possible.
The admin urls follow a pattern admin:<app>_<model>_<action>, so if you had a model named Foo in an app named bar you would access the change admin URL with the following reverse
reverse('admin:bar_foo_change', kwargs={'object_id': object.pk})
After I login, I need to redirect to another page while adding URL parameters to the URL of the next page. I get the value of these parameters after the user is authenticated because they need to be accessed from the user database table. I heard about using the next parameter but I don't know how I would use it since I need to access the database table and I can't do that from urls.py. This is my url.py line for login right now:
url(r'^$',auth_views.login, name='login',kwargs={
'authentication_form':loginPlaceHolderForm,
}),
I'm not really sure what other info you need so just ask for it in the comments and I'll be sure to add it.
Also I'm using Django 1.11
EDIT:
For more clarification: What I want is something like this /colors?team=blue
And let's say the team can be red, blue or green and you get this value from the team column in the given row that you get when the user logs in.
You could try to override djangos class-based view LoginView.
In views.py
from django.contrib.auth.views import LoginView
class MyLoginView(LoginView):
authentication_form = loginPlaceHolderForm
def get_redirect_url(self):
print(self.request.user)
# get data for user here
user_data_query_string = ''
url = '{}?{}'.format(
reverse('app:some-name')
user_data_query_string)
return url
In urls.py
url(r'^$', MyLoginView.as_view(), name='login'),
See also this question about adding GET querystring parameters to djangos HttpRedirect.
Hey there I try to use userena with django to build a website which has a clean dashboard after login in. My current problem is that userena uses a pretty high url depth.
Userena is under myproject/accounts and uses the url 'user'. Later 'dashboard'.
When logged in 127.0.0.1:8000/user/ is the userena url. In the normal state it lists all registred users. I've managed to change that to show the current logged in users profile with (accounts/urls.py):
url(r'^$', views.dashboard, name='dashboard'),
url(r'^', include('userena.urls')),
My problem is now that I want to change the normal userena urls.
Userena urls are:
127.0.0.1:8000/user/username/signout
127.0.0.1:8000/user/username/edit
127.0.0.1:8000/user/username/email
127.0.0.1:8000/user/username/password
....
I want:
127.0.0.1:8000/user/signout
127.0.0.1:8000/user/edit
....
I've tried to change both the url and the view but I get always the GuardianError.
Url change:
url(r'^edit', userena_views.profile_edit),
View change:
url(r'^edit', views.settings, name='settings'),
+
def settings(request):
user = request.user
response = userena_views.profile_edit(request, user)
return response
The error:
There are probably multiple ways to archive this. Thank you for the help and sorry for the poor english.
I found a method to solve this here http://tundebabzy.blogspot.de/2013/04/an-easy-way-to-override-third-party-app.html
I practically changed the view like in the tutorial to serve the same files like the original userena view. The case that there will be no username had me to add that extra in the view.
def profile_edit(request, edit_profile_form=userena_forms.EditProfileForm,
template_name='userena/profile_form.html', success_url=None,
extra_context=None, **kwargs):
username = request.user
return userena_views.profile_edit(request=request, username=username,
edit_profile_form=edit_profile_form, template_name=template_name,
success_url=success_url, extra_context=extra_context)
Url:
url(r'^edit/$', views.profile_edit, name='userena_profile_edit'),
So I have two models in the same app that have pretty much identical url structures:
urlpatterns = patterns('',
#....
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
url(r'^(?P<company>[\w-]+)/(?P<slug>[\w-]+)/$', 'PrizeItem_Index', name="prizepage"),
))),
# Old Redirects
)
The problems here being Reviews and PrizeType. I want my urls structured so that a user looking for prizes under a certain category goes to /prizes/prizetype. But if they want to see prizes under a certain company, then they'd go to /prizes/companyslug/. However, these two urls will naturally conflict. I can always just change the url structure, though I'd rather not. I just want to know if there are any ways around this that don't involve changing the url structure.
I would suggest writing a helper view function, which checks whether the inputted url corresponds to a company or a category, and then redirecting the request to the appropriate page.
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', prizehelper, name="prizehelper),
where, you can check within prizehelper, if it is a company or a category and move on accordingly.
Another approach could be, to change your url structure, and reflect which type of url it is
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^company/(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^category/(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
Have a single urlconf entry that goes to a view which figures out which type is being examined and then dispatches to the appropriate view for that type.