Customize Installed Apps on Django - django

in my server I have a lot of apps installed like, facebook_connect, userena, guardian and so on...
For example, I realized that if I customize the:
django-userena / userena / templates / userena / emails / activation_email_message.txt
{% load i18n %}{% autoescape off %}{% load url from future %}
{% if not without_usernames %}{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
{% endif %}
{% blocktrans with site.name as site %}Thank you for signing up at {{ site }}.{% endblocktrans %}
{% trans "To activate your account you should click on the link below:" %}
{{ protocol }}://{{ site.domain }}{% url 'userena_activate' activation_key %}
{% trans "Thanks for using our site!" %}
{% trans "Sincerely" %},
{{ site.name }}
{% endautoescape %}
For specified website, and I have more than 4 in the same server, I will make a complete mess in my django_site.
My question is:
How to customize the templates or models in some installed apps without completely change the original django_site?
Thanks in advance,

You cannot change the models, but you can override templates.
In the same directory as manage.py, you would have a directory called templates, there, you can create the following folder hierarchy, and put your custom template.
templates/userena/emails/activation_email_message.txt

Related

Django 3: Badly formed URL in password_reset_email.html

I have a Django 3.0.7 app using the default template for sending password reset emails.
For some of our users, the link to reset is badly formed, e.g.:
https://foo.bar/accounts/reset/McU4Mw/5no-9cd52590f0943503=
fa3a/
/>
While it should be:
https://foo.bar/accounts/reset/McU4Mw/5no-9cd52590f0943503fa3a/
This second one is whats actually being sent. But, as I said, some users are receiving the first one.
I assume some of my users' email clients are wrecking the text, but what could I do?
I've considered adding a plaintext url to the email, but it's already plaintext.
EDIT: This is a solution I came up with, and it works. Can anyone think of a reason not to do this?
urls.py
...
path('accounts/reset/<uidb64>/<token>/<str:extra_chars>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('accounts/reset/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
...
EDIT:
Here's the default template:
/site-packages/django/contrib/admin/templates/registration/password_reset_email.html
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}

Django: Different LOGOUT_REDIRECT_URL Workflow for Admin than the normal User

In my Django App settings I've mentioned the following 2 lines in settings.py, it works fine for the normal user but is not good enough for the Admin.
LOGIN_REDIRECT_URL = '/my_app/profile/'
LOGOUT_REDIRECT_URL = '/my_app/login'
When I log out from the Admin, it takes me to the login page of the user, as mentioned in the above LOGOUT_REDIRECT_URL. What I want is that admin logout redirect should go to the Admin login page, not the user login page.
snippet from urls.py
path('admin/', admin.site.urls),
Can anyone suggest the best way to deal with this issue?
EDIT:
I tried to extend Django's Admin base.html which contains the Logout part.
{% block userlinks %}
{% if site_url %}
{% trans 'View site' %} /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
{% trans 'Change password' %} /
{% endif %}
{% trans 'Log out' %}
{% endblock %}
How can I change this admin:logout functionality so that it redirects to admin login page?

NoReverseMatch at /user_details/reset-password

I know it's well asked, but somehow none of the other answers seemed to solve my problem.
I get an error with the ReverseMatch, here is the relevant urls.py section:
# for password reset
url(r'^reset-password$','django.contrib.auth.views.password_reset', {'post_reset_redirect' : 'user_details/reset_password.html', 'template_name': 'user_details/reset_password.html'}, name="password_reset"),
url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', {'post_reset_redirect' : 'user_details/reset_password.html'}, name="password_reset_confirm"),
I think the name is adequately defined?
The problem is in the template /usr/lib/python2.7/dist-packages/django/contrib/admin/templates/registration/password_reset_email.html, error at line 6:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
where the {% url ...} is highlighted in red.
Why?
I assume that Django would then use the default template for that view?
Thanks!
Chris
Right, I had to resolve to overwriting the Django default email, because the reverse URL lookup doesn't want to behave properly.
So I created a copy of the original Django email file (/usr/lib/python2.7/dist-packages/django/contrib/admin/templates/registration/password_reset_email.html):
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
and replaced line 6 with the following:
{{ protocol }}://{{ domain }}/user_details/reset/{{uid}}/{{token}}
which matches my urls.py.
This does indeed work now...
You have defined a URL which accepts a "uidb36" parameter, but the reverse call is passing "uidb64".

How do I add urls to {% blocktrans %} without failing silently

For adding an url to a blocktrans, I use:
{% url "home" as home %}
{% blocktrans with url=home %}
...
{% endblocktrans %}
However, this is not equivalent to use {% url "home" %} without the blocktrans because it fails silently if url "home" doesn't exist.
Is there any way to add an url to blocktrans as if I was using {% url "home" %} inside the {% blocktrans %}?
Putting in other words, how do I make a sentence with an url translatable and having the url not failing silently in Django?

Django blocktrans error

I am nearing the final stages of a project and have run into a bit of a hiccup with Django.
It relates to the {% blocktrans %} tag.
How do I enable it to be fully functional in my app, currently if I wrap a piece of text in {% blocktrans %} I get a TemplateSyntaxError message
I have the following in my
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.i18n",
...
)
Any help would be appreciated.
For me was like this (windows + python 2.6 + django 1.2.1)
Will result an error(TemplateSyntaxError):
{% load i18n %}
{% blocktrans %} My name is {{ user.firstname }} {% endblocktrans %}
Will work:
{% load i18n %}
{% blocktrans with user.firstname as hmpf %} My name is {{ hmpf }} {% endblocktrans %}