I want to pass the account information to the template so that when an user account is activated, there is a message saying "your account is activated; please log in now" with a link below. if the activation days have expired, it must say "activation days expired". I have url and template here, but I do not know how to pass the account information to the template.
url.py
urlpatterns = patterns('',
url(r'^activate/complete/$',
TemplateView.as_view(template_name='registration/activate.html'),
name='registration_activation_complete'),
......)
registration/activate.html
{% extends "registration/base.html" %}
{% block title %}Account activated{% endblock %}
{% block content %}
<h1>Account activated.</h1>
{% load humanize %}
{% if account %}
<p>Thanks for signing up! Now you can log in.</p>
{% else %}
<p>Sorry, it didn't work. Either your activation link was incorrect, or
the activation key for your account has expired; activation keys are
only valid for {{ expiration_days|apnumber }} days after
registration.</p>
{% endif %}
{% endblock %}
basically, I want to pass account and expiration_days to the above template. I just don't know how. help plz!
I'm afraid it won't be enough just to use TemplateView like you did; you'll have to write a custom view which will contain the logic for activation and pass the data to the template.
Related
I am using a custom e-mail message by overriding the default confirmation email of Django Allauth email_confirmation_message.txt
I have a custom signup form that also collects first and last name so I would like to access the first name that they have given during signup, in my e-mail confirmation. So you can address someone with his first name.
This is what the default template looks like:
{% load account %}
{% user_display user as user_display %}
{% load i18n %}
{% autoescape off %}
Dear {{ first name given at registration here }}
...
{{ activate_url }}
...
{% endautoescape %}
It uses {% user_display user as user_display %} to display the username. So there must be a way to access the first name as well?
How can I access the first name in this e-mail? Or is this not possible without completely writing a custom confirmation e-mail?
I need the search result to be validated together with the statement that it is a query inside the template.
So if the user is searching for another user and this user does not exist, it returns nothing. otherwise it returns the username of the user.
If request.user just checks the user that is logged in.
something like this
{% if request.GET.q and request.GET.q.is_authenticated %}
but this does not work :D Thank you
Can I use is_authenticated or is there a better way?
<li class="dropdown-hover">
<form class="form-inline">
{% include "tweets/search_form.html" %}
</form>
{% if user in request.GET.q.is_authenticated %}
<div class="dropdown-content x-card-4 x-bar-block" style="width:300px">
<a href='{{ request.GET.q }}'>{{ request.GET.q }}</a><br/>
{% else %}
<div class="dropdown-content x-card-4 x-bar-block" style="width:300px">
<a href='#'>No users found</a><br/>
{% endif %}
</li>
Thank you for any help
There is a better way, just do {% if request.user.is_authenticated %}.
you need to add loginRequiredMixin in your class so it allow to perform tasks only if user is logged in otherwise it redirect to login page.
#andre If you want to show/hide html tags in a template to the guest/logged-in user, you can use this {% if user.is_authenticated %} or you want to show some pages to the logged-in user #tabish-manzoor's solution is great.
I am pretty new to Flask/Flask-Admin.
I have followed the tutorial on flask admin and managed to get the admin panel working but slightly lost on how to get the below things implemented.
https://github.com/flask-admin/flask-admin/tree/master/examples/auth
When logged in as a normal user I can only see "home" page.
How can I expose other views to "normal user" and restrict actions such as read only etc.
I have created a "baseview" which is not associated with any other models as below:
class SitesView(MyBaseView):
#expose('/')
def index(self):
return self.render('views/testviews.html')
admin.add_view(SitesView(name='Test views', endpoint='test views'))
and html as below:
{% extends 'admin/master.html' %}
{% block body %}
{{ super() }}
{% if current_user.has_role('view1') %}
Site1
{% endif %}
{% if current_user.has_role('view2') %}
<a>Site2</a>
{% endif %}
{% if current_user.has_role('view3') %}
<a>Site3</a>
{% endif %}
{% if current_user.has_role('view4') %}
<a>Site4</a>
{% endif %}
{% endblock %}
This gives me a new tab with different views with works as expected.
What I am trying to achieve here is when user click the Site1 link they go to Site1 page within flask-admin interface but I am not sure how to do that. I could create a new route for this but the problem is I can't(don't know how to) extend flask admin template.
For example this works but it redirect the page outside flask-admin template:
#app.route('/views/')
def views():
return render_template('views/views1.html')
and modified the templates>admin>index.html page with below:
<ul class="lead text-center list-group">
{% if current_user.has_role('view1') %}
<li class="list-group-item">View1</li>
{% endif %}
{% if current_user.has_role('view2') %}
<li class="list-group-item">View2</li>
{% endif %}
{% if current_user.has_role('view3') %}
<li class="list-group-item">View3</li>
{% endif %}
{% if current_user.has_role('view4') %}
<li class="list-group-item">View4</li>
{% endif %}
</ul
I want to build the whole web site using flask admin so that I can keep user experience consistence. Am I doing this the wrong way?
Thanks for your time.
Please do let me know if you want me to provide more information on this issue.
Kind Regards.
So after going through documentations and tutorials I have found the solution to my issue.
For my first question:
When logged in as a normal user I can only see "home" page. How can I
expose other views to "normal user" and restrict actions such as read
only etc.
We can do this by overwriting our view functions is_accessible method as below:
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1'):
return True
return False
For my second question we just need to give the endpoint as for our BaseView as below:
class MyView(BaseView):
#expose('/')
def index(self):
return self.render('views.html')
admin.add_view(MyView(name='Custom Views', endpoint='customviews'))
And then in your jinja template you need to call it:
href="{{ url_for('customviews.index') }}
Just one thing to note, doing this:
current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1')
could get quite messy if we have so many roles, not sure how we would approach this but hoping this will help someone.
Thanks all.
I know this is an old question, but for the following code
current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1')
What I like to do is having a hybrid_property (available on both Peewee and SQLAlchemy) inside my User class that consolidates these properties. So it'd look something like this:
#hybrid_property
def user_has_administrative_rights(self):
return self.has_role('superuser') or self.has_role('user')
newbie django question
I'm using django-profiles to display a list of profiles, using profiles.views.profile_list view, and individual profiles, using profiles.views.profile_detail.
For each individual profile, I wanted to add a link to edit the profile, only if the profile corresponds to the current user. My first attempt was:
(...)
{% block content %}
<p>Profile detail info for {{ user }} </p>
<ul>
<li>{{ user.username }}
{% if user.is_authenticated %}
edit
{% endif %}
</li>
<li>{{ user.get_profile.url }}</li> (...)
But then I hit the obvious issue: edit_profile is meant to edit the current user profile, so doesn't take username/ id as arguments, and django cannot reverse the url, because in fact I don't have a url to edit a named user's profile.
What's the best strategy here? copy the profiles app into my project and add the named user edit profile view? Or is there a way to do it without having to have a local project copy of the app - which I guess would be preferable?
thanks
The edit_profile view already enforces that the profile being edited is the current user's profile. You don't need to pass any arguments to it:
(...)
{% block content %}
<p>Profile detail info for {{ user }} </p>
<ul>
<li>{{ user.username }}
{% if user.is_authenticated %}
edit
{% endif %}
</li>
<li>{{ user.get_profile.url }}</li> (...)
I use the obvious powerful registration 0.8 alpha version. I do have to questions.
1.) I want to add the checkbox RegistrationTermsOfService at the registration form html file. I just don't know how to do it. Please don't give me an link on uebernstorm docs. Just tell me please how to do it. I have read and tried a lot and it actually doesn't work. I am dispairing!
2.) The activation email is send with the activation key. When I click on activation the account is getting activated in the database but the template says something different. It says:"Sorry, it didn't work. Either..."
I use the standard activate.html below:
{% extends "base.html" %}
{% block title %}Account activated{% endblock %}
{% block content %}
Account activated
{% load humanize %}
{% if account %}
Thanks for signing up! Now you can "href"
{% else %}
Sorry, it didn't work. Either your activation link was incorrect, or
the activation key for your account has expired; activation keys are
only valid for {{ expiration_days|apnumber }} days after
registration.
{% endif %}
{% endblock %}
PLEASE, PLEASE help me!!! I am dispairing!!!
Craphunter
For 1) you need to use a different form. Look in registration.forms for the one you need and pass it as an additional argument in the backend's urls.py
For 2) we'll need more info - is the URL correct? Is there a RegistrationProfile object created for the user account in question? What is the value of the key?