django problem:captured parameter in parent urlconf , incude() and reverse match - django

If I want to have django app called app1 which can make a user post note to someone's page and to his/her own page.
And if the user is logged in ,no need using username in the url, but the username parameter in url can be acts as parent in urlconf.
To make it clear:
urls.py
urlpatterns = patterns(''
#if registered user/anonymous user visit someone's page
url(r'^/foo/users/(?P<username>\w+)/app1/',include('app1.urls', namespace='myapp1')),
#if user is logged in in his own page
url(r'^app1/', include('app1.urls', namespace='myapp1')),
...
)
app1/urls.py
urlpatterns = patterns('',
# I expect this pattern receives the username parameter from above
url(r'^note/add/$', app1_views.add_note,
name='add_note'),
url(r'^note/add/$', app1_views.add_note,
{ 'username':None}, name='add_note_own'),
...
...
)
app1/views.py
def add_note(request, username=None):
...
...
First question:
Now for example john is logged in and on jack's notes page john want to post a note.
I want to be able to do something like this or near something like this:
template app1/notes.html
{% if request.user.is_authenticated %}
{%if in his/her own note page %}
add note Expected generated url: www.domain.com/app1/add
{%else}
add note Expected generated url: www.domain.com/foo/jack/app1/add
{%endif%}
{% endif %}
Is this possible?
Another thing,
if john wrote note in jack's page, and django gives the note id == 3,
so to show that note, only these urls are valid:
www.example.com/foo/jack/app1/3
www.example.com/foo/app1/3 (if jack logged in)
Second question:
what I want to achieve is reverse match can accept captured parameter up to to the
parent urlconf when include() is involved in url configuration.
Can this be done?
Or if you get what I mean and can provide simpler solution , please do so :)
sorry if this post is confusing, I am confused myself.
Thanks alot for the patience.
I'm using django 1.2.5

you need to pass an username, one example can be:
urlpatterns = patterns('',
# I expect this pattern receives the username parameter from above
url(r'^note/add/(?P<username>[^/]+)/$', app1_views.add_note, name='add_note'),
# username is an optional argument, so no need to pass it
url(r'^note/add/$', app1_views.add_note, name='add_note_own'),
)
and then in template:
{% if request.user.is_authenticated %}
{% if page.owner == request.user %}
add note
{% else %}
add note to {{ request.user.username }}
{% endif %}
{% endif %}

Related

Django - add link with custom admin page href

In my Django project, I have created a custom admin page for an app via the get_urls() method. I'd like to add a link to the app's main model index view that will take users to this custom page - however, I'm having some trouble creating this link element correctly and I don't seem to be able to piece together the right way to do it - I'm just left with a Reverse for 'export' not found. 'export' is not a valid view function or pattern name. error.
I've set up the admin for the app like so:
# my_project/observations/admin.py
from django.template.response import TemplateResponse
from django.urls import path
class ObservationAdmin(SimpleHistoryAdmin, SoftDeletionModelAdmin):
change_list_template = 'export_link.html'
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('export/', self.admin_site.admin_view(self.export_view), name='export')
]
return custom_urls + urls
def export_view(self, request):
context = dict(
self.admin_site.each_context(request),
)
return TemplateResponse(request, 'export.html', context)
and the two templates that are referenced:
# my_project/observations/templates/export.html
{% extends "admin/base_site.html" %}
{% block content %}
<div>
Some custom content
</div>
{% endblock %}
# my_project/observations/templates/export_link.html
{% extends 'admin/change_list.html' %}
{% block object-tools-items %}
<li>
Export
</li>
{{ block.super }}
{% endblock %}
Navigating directly to http://localhost:8000/admin/observations/observation/export/ works perfectly, I see the custom content page exactly as I want it... so the issue I'm striking is with the link template - I get the Reverse... error when I navigate to the model index page.
Perhaps the argument I'm passing to url is incorrect, or I need to register that URL elsewhere - but I don't quite know. The other examples of link elements like this that I've been able to find don't reference URLs created via the admin class' get_urls() method - so any guidance on this would be greatly appreciated.
Thanks very much, let me know if there's any other info that I can provide to help sort this out.
I think the problems is in missing namespace in your export_link.html template. Instead of:
Export
try:
Export

Build up url in django

Im making a applikation in django and im totaly new to to language python and framework django.
My problem is i can't figure out how to get this url:
project/2/ticket/1
Im in project/2 and got an link in my template that links to project/2/ticket/1.
The error i get is NoReverseMatch at /project/1 and the code im using is:
url.py
urlpatterns = patterns('',
# Project urls below
url(r'^project/(?P<project_id>\d+)$', 'project_manager.views.project_list', name='project_list_with_ticket'),
url(r'^project/(?P<project_id>\d+)/ticket/(?P<ticket_id>\d+)$', 'project_manager.views.ticket_list', name='ticket_list'),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
views.py
def ticket_list(request, ticket_id = None):
if ticket_id:
tickets = get_list_or_404(Ticket.objects.filter(id = ticket_id))
return render(request, 'tickets/details.html', {"tickets" : tickets })
else:
return render(request, 'projects/list.html', {'projects' : projects })
my template
{% for ticket in tickets %}
<div class="tickets">
{{ ticket }}
</div>
{% endfor %}
EDIT --
It says there is no ticket_list, but there are?
You are calling the ticket_list URL/View with one variable - ticket.id - when it requires two.
Your template:
{{ ticket }}
You are passing a ticket.id and it also requires a project.id as you can see in your URL:
url(r'^project/(?P<project_id>\d+)/ticket/(?P<ticket_id>\d+)$', 'project_manager.views.ticket_list', name='ticket_list'),
You need to change your template to pass both variables that you are asking for in the URL. You haven't posted how you get the project id, so my code will probably not work exactly as posted, but you need something like this:
{{ ticket }}
The order that the variables are passed need to align with the way that they are asked for in your URL. Since your URL asks for a project_id and then a ticket_id, make sure you pass them in your template in that order as outlined above.

How get current user in a template tag?

How can i get the current user in a django template tags? (request object is not accessible)
Or how can i access to request object?
If you want to access the current user in a template tag, you must pass it as a parameter in the templates, like so:
{% my_template_tag user %}
Then make sure your template tag accepts this extra parameter. Check out the documentation on this topic. You should also check out simple tags.
The user is always attached to the request, in your templates you can do the following:
{% if user.is_authenticated %}
{% endif %}
You don't have to specify "request" to access its content
UPDATE:
Be aware: is_authenticated() always return True for logged user (User objects), but returns False for AnonymousUser (guest users). Read here: https://docs.djangoproject.com/en/1.7/ref/contrib/auth/
This question was already answered here:
{% if user.is_authenticated %}
<p> Welcome '{{ user.username }}'</p>
{% else %}
Login
{% endif %}
and make sure you have the request template context processor installed in your settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.request',
...
)
Note:
Use request.user.get_username() in views & user.get_username in
templates. Preferred over referring username attribute directly.
Source
This template context variable is available if a RequestContext is used.
django.contrib.auth.context_processors.auth is enabled by default & contains the variable user
You do NOT need to enable django.core.context_processors.request template context processor.
Source : https://docs.djangoproject.com/en/dev/topics/auth/default/#authentication-data-in-templates
Suppose you have a profile page of every registered user, and you only want to show the edit link to the owner of the profile page (i.e., if the current user is accessing his/her profile page, the user can see the edit button, but the user can't see the edit button on other user's profile page.
In your html file:
<h2>Profile of {{ object.username }}</h2>
{% if object.username == user.username %}
Edit
{% endif %}
Then your urls.py file should contain:
from django.urls import path
from .views import ProfileUpdateView
urlpatterns = [
...
path('<int:pk>/profile/update', ProfileUpdateView.as_view(), name = 'profile_update'),
...
]
considering you have appropriate ProfileUpdateView and appropriate model

How to reset user password from the admin interface

In my website, I want to let the admins reset the password of any user.
With reset I mean exactly what the password_reset view does (under contrib.auth): Send a confirmation link to that user email.
How would be the best way of doing that? Is there an already app/snippet that does that?
Edit:
Let's suppose user john is an admin. What I want is to let john reset any user's password through the admin interface. For example, to reset max password, he will just go to the max user, and click on any link to reset his password.
What I finally did was to add a custom ModelAdmin:
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.admin import UserAdmin
class CustomUserAdmin(UserAdmin):
...
def reset_password(self, request, user_id):
if not self.has_change_permission(request):
raise PermissionDenied
user = get_object_or_404(self.model, pk=user_id)
form = PasswordResetForm(data={'email': user.email})
form.is_valid()
form.save(email_template_name='my_template.html')
return HttpResponseRedirect('..')
def get_urls(self):
urls = super(UserAdmin, self).get_urls()
my_urls = patterns('',
(r'^(\d+)/reset-password/$',
self.admin_site.admin_view(self.reset_password)
),
)
return my_urls + urls
and I also had to override the change_form.html template, like this:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
{% if change %}{% if not is_popup %}
<ul class="object-tools">
{# You can also give a name to that pattern and refer to it below using 'url' #}
<li>Reset password</li>
<li>{% trans "History" %}</li>
{% if has_absolute_url %}
<li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink">
{% trans "View on site" %}</a>
</li>
{% endif%}
</ul>
{% endif %}{% endif %}
{% endblock %}
The result looks like this:
If you want a more detailed explanation, I blogged about it.
The passreset app just exposes the django views via urls.py, and adjusts the login template to show a "Forgot my password" link.
The built-in django password reset views and templates are meant for self-reset. I guess the reset form could be prepopulated with a different user's email address (in the query string) but you'd still need to make adjustments such as changing the email template - "You're receiving this e-mail because you requested a password reset for your user account" is probably not what you want:
https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/templates/registration/password_reset_email.html
Therefore you should expose the views at different URLs if you want to include self-reset as well.
Hook the django views into urls.py like so:
urlpatterns += patterns('django.contrib.auth.views',
url(r'^accounts/password/reset/$',
'password_reset',
name='password-reset'),
url(r'^accounts/password/reset/done/$',
'password_reset_done',
name='password-reset-done'),
url(r'^accounts/password/reset/confirm/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$',
'password_reset_confirm',
name='password-reset-confirm'),
url(r'^accounts/password/reset/complete/$',
'views.password_reset_complete',
name='password-reset-complete')
)
and where you want to make adjustments, pass in e.g. your own email template:
url(r'^/accounts/password/reset/$',
'password_reset',
{'email_template_name': 'my_templates/password_reset_email.html'}
name='password-reset'),
The "password_reset" view has more parameters you can tweak:
https://docs.djangoproject.com/en/dev/topics/auth/#module-django.contrib.auth.views
("post_reset_redirect" comes to mind as another one for your purposes)
To show a corresponding link you'd either change the User admin (careful, already registered - unregister then register your own, subclassed plus additional link field) or the change_form template itself.
I'm unaware of an app that provides this out-of-the-box, so I upvoted the question :-).
Yep, there is an app for that. Check here:
https://github.com/bendavis78/django-passreset

How can I generate a url to a particular item in the Django Admin Site from a view?

I would like to make a link that would take the user to a particular item in the admin site (assuming they have the correct permissions).
Something like: https://mysite/admin/app/model/id/
Can this be done with reverse?
You can get the url in the view, using reverse,
object_change_url = reverse('admin:myapp_mymodel_change', args=(obj.id,))
Or in the template, using the url tag
{% url 'admin:myapp_mymodel_change' obj.id %}
or
{% load admin_urls %}
{% url opts|admin_urlname:'change' obj.id %}">
Note the above url tag syntax is for Django >= 1.5.
For more information, see the Django docs on reversing admin urls.