Redirect different user profiles to different pages? - django

How can "http://my-domain.com/users" return a unique profile page for each user?

RedirectView in Django is normal view. It can take parameters from URL dispatcher, do some logic and decide where to redirect, based on that parameters (or even something more).
New URL can be fetched from database, built using parameters from dispatcher or even randomized.
You can also use old, function based view and simply return HttpResponseRedirect.

Related

How to append dyanmic url pattern at the beginning of fixed tail in django urls?

I want to give AJAX call to dyanmic urls in django to serve images. An individual profile can have it's own photos. Then team of individuals will display photos of all members and main page may highlight different random images from different teams. So, I can have following set of urls in django:
^home/images/
^home/teams/1/images/
^home/teams/1/user/2/images/
I'll be using AJAX request to send details like team id, user id etc. So, there'll be only one view that'll handle any request related to viewing images. How can I route all above url request to this single view with single url pattern.
To be specific, How can I construct dynamic urlpattern in django which will allow me to append '/images/' to any url at the run time? Means visitor should be routed to the same view whether he visits any of the above url?
If there is any flaw in my logic or approach, please correct me as I am not expert in django. I'm using django 1.9 version.

Django login_required redirect with hash [duplicate]

I am using django-braces's LoginRequiredMixin in a Django 1.6 project. This mixin replicates Django's login_required decorator.
I have a view that uses the LoginRequiredMixin that has a URL like this: /spa_home/#price_requests/68. If I try to hit this URL without being logged in, the mixin correctly sends me to the login page with a request like this: /accounts/login/?next=/spa_home/#price_requests/68. Unfortunately, after successfully logging in, the URL hash fragment is left off and I am just redirected to /spa_home/.
What is the best way to fix this? Removing hash fragments from my application would be a large effort.
The issue is the way the browser interprets the login URL. You want it to be intepreted like this:
/accounts/login/?next="/spa_home/#price_requests/68"
but actually, it is seen like this:
"/accounts/login/?next=/spa_home/"#price_requests/68
In other words, the hash is seen as attaching to the login URL itself, not the redirect parameter.
The way to fix this is to quote the parameter:
urllib.quote('/spa_home/#price_requests/68')
which gives you /spa_home/%23price_requests/68, which will be interpreted correctly.

Using django-registration forms in custom templates

I'm trying to create an account settings page for logged-in users. One of the things users should be able to do is to change their password.
I'm using django-registration and it provides a password change form at site/accounts/password/change by default, and it works. However, I want this function to be available at an Account Settings page instead, along with other administrative functions.
I first tried copying the template code, but it did not work because it includes a special form to create the inputs for the passwords (to handle validation). I don't know how to include this form in my own template.
How can I recreate these forms in my own Account Settings template?
This is the default password change template. I want to reuse form.oldpassword and the others in a separate template.
Django-registration doesn't implement its own password change view, it reuses the one included in Django (django.contrib.auth.views.password_change). It is hooked in through registration.auth_urls which is included in the default and simple registration backends.
By default the view uses django.contrib.auth.forms.PasswordChangeForm (which can be overridden through the password_change_form parameter).
When you only reuse the form (which you can do of course, just import the above form in your custom view), you should be aware that you would still be missing the whole view logic. So unless you have a more complex view in mind, you should consider to reuse it and just override the registration/password_change_form.html and registration/password_change_done.html templates.

Django URL parameters from external apps

I am using Django and I have the following URL in my project's urls.py file.
(r'^user/(?P<username>[\w_\-\.]+)/my_app/', include('my_app.urls')),
(r'^user/(?P<username>[\w_\-\.]+)/my_other_app/', include('my_other_app.urls')),
...
The goal is to have an application that uses the username of a user e.g. a profile application where every user has a profile page. Only one view in this application needs to have the username in the URL (the one that renders the profile page), but all the views must take a username parameter even if they don't do anything with it.
I suspect that this usage of URL parameters is wrong because it forces every view of my_app to take username as a parameter (because it's passed as from the URL dispatcher). To me it doesn't make sense for an external component (the project's urls.py file) to tell my_app's URLs what parameters to take.
Is this usage correct? If not, how should I do this?
It does seem kind of odd.
Could you not switch your apps to look for the username parameter instead? Under my_app.urls and the latter, just have the username field in the lookup on the fields you need.
That way your structure looks more like:
/user/my_app/friends/hekevintran/
/user/my_other_app/connections/bartek/
Which works just as well and doesn't force your apps to rely on the context of your root project for the username.

Redirecting to a page

I am facing a problem: I want to give a link in my change form that will redirect to a page which may be simple php page also or any page, in that page I want to perform some db queries and display them. I also wan to pass id on click. Is it possible?
In my view.py I wrote:
from django.shortcuts import render_to_response
from django.template import RequestContext
def MyClass(self,id,request):
return render_to_response('admin/custom_change_form.html')#my template location
My model and admin files are simple.
To send to a file directly, use direct_to_template(). You can pass anything in the url that you like - just give the information to your template, and write it in the url. After all, Django doesn't require url helpers.
I sense that whatever you're trying to do is some god-awful hackish thing that would be much better served by doing it all in Django.
You will need to override your change form for that model and display whatever you would like. But that is making the assumption you are talking about the contrib admin within Django.
Realistically you have not provided sufficient information for anyone to accurately answer your question.