the code {{ request.user.first_name }} used on a HTML page with Django works perfectly in Chrome. However, it is not working in Safari and just displays nothing.
Is there any alternative way of getting this or how can I fix this?
Not using Safari is not an option as people with Safari will use this web-app.
EDIT:
Minimum reusable example:
base.html
<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
Logout
{% else %}
Log-in
{% endif %}
</span>
How it looks on Chrome:
Safari:
I assume that you are logged in with a different user in Safari, which this user doesn't have a value in first_name.
Please double check that your are logged in with a user have a valid first_name.
Related
I am having problem with facebook login app integration the problem is
'auth' is not a registered namespace
the problematic part is the:
<p>
{% if user and not user.is_anonymous %}
Hello {{ user.get_full_name|default:user.username }}!
{% else %}
I don't think we've met before
{% endif %}
</p>
Login
Logout
I got that auth is not recognized but in the tutorial there were no problem with that. I am using django versio 1.9.5.
In my HTML code i have the following jinja template code:
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
The issue is: let's assume that {{ site.site_link }} = www.domainname.com, so whenever the link is clicked it supposed to direct the user to the domainname page. but, it leads to :
http://127.0.0.1:5000/www.domainname.com
which is a 404..Any idea why is this happening and how to fix it?
I don't know if this is relevant, but, I am using flask Blueprints.
You need to put http:// in front of it, like href="http://........"
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
I am working on what might become a sort-of kiosk app. I am new to python and django but it is rolling along. My allauth flow for signup uses either a social login (Google for the moment) or a "local" email address & password.
If I login with a Google account then logout I am redirected to the sign-in page, cool. The thing is I have not really been logged out of the Google account. If I click the social login link then I am back in the user area with no password challenge.
Does allauth have a way to logout and have the social auth token removed? Do I need to catch the logout signal and find/delete the token myself?
Looks like there is a built-in solution. There is an action parameter that can be given the value "reauthenticate". Being new to this stuff I am not positive that I have added it in the python/django way but I have edited the template:
myProject/templates/allauth/socialaccount/snippets/provider_list.html
and added action=reauthenticate" to the social auth href line a la:
{% load socialaccount %}
{% get_providers as socialaccount_providers %}
{% for provider in socialaccount_providers %}
{% if provider.id == "openid" %}
{% for brand in provider.get_brands %}
<li>
<a title="{{brand.name}}"
class="socialaccount_provider {{provider.id}} {{brand.id}}"
href="{% provider_login_url provider.id openid=brand.openid_url process=process action='reauthenticate' %}"
>{{brand.name}}</a>
</li>
{% endfor %}
{% endif %}
<li>
<a title="{{provider.name}}" class="socialaccount_provider {{provider.id}}"
href="{% provider_login_url provider.id process=process scope=scope auth_params=auth_params %}">{{provider.name}}</a>
</li>
{% endfor %}
That seems to do the trick.
I am using django-registration. After getting it working, I went on extending views and forms for field customization using Bootstrap.
I have this working for everything except the logout page. In the default method, I simply built the form by scratch and it worked OK. But when trying to use Django's forms, it's not showing the fields at all. Both the login and logout templates use this basic form:
<div class="auth-form">
<form class="form-signin" method="post" action="/accounts/login/">
{% csrf_token %}
<h2 class="form-signin-heading">Please log in</h2>
<div class="font-16px top-mar-1em">
<label for="id_username"></label>
{{ form.username }}
<label for="id_password1"></label>
{{ form.password }}
<div class="form-group">
<div class="col-sm-10">
<div class="level-checkbox">
<label for="checkbox">
{{ form.remember_me }} Remember me
</label>
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Log in</button>
</div>
</form>
</div>
This works fine for the login page, but for the logout page it doesn't (the fields are missing). It's like the form is not available to this page. I'm sure I missed something simple but I can't figure it out.
My urls.py section look like this:
url(r'^accounts/login/$','django.contrib.auth.views.login', {
'template_name': 'registration/login.html',
'authentication_form': UserAuthenticationForm,
}),
I've tried adding a logout section to urls.py similar to the login one, but that hasn't worked. I've tried using logout_then_login, using login.html as import template on logout.html and other miscellaneous things to no avail.
Apparently, there is no form associated with the default logout view, so this is probably the issue but I'm not sure the best way to implement a standard form in the logout view.
You should be able to add the following to your urls.py and have everything work.
url(r'^accounts/logout/$','django.contrib.auth.views.logout_then_login')
Make sure you have LOGIN_URL defined your settings.py. Then when a user clicks a logout link they will hit the logout view which takes care of redirecting to the login view which contains your form.
Thanks to #tsurantino I was able to get this working using the messages framework. I always like when I have to implement a new feature using a part of Django I haven't used before. Adding to the toolkit, so to speak. Here is what I did:
Since I had a signal_connectors.py file already for sending email based on user registration/activation I just added to it:
#receiver(user_logged_out)
def user_logged_out_message(sender, request, **kwargs):
messages.add_message(
request,
messages.INFO,
'You\'ve been successfully logged out. You can log in again below.'
)
Then in my common app's __init__.py I imported it:
from common.signal_connectors import user_logged_out_message
Then in my login.html template:
{% if messages %}
<div class="alert-success align-ctr font-16px top-mar-2em">
{% for message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
Finally, to redirect to login page after logout (urls.py):
url(r'^accounts/logout/$','django.contrib.auth.views.logout_then_login'),
Thanks for everyone’s help and comments.
I have following code in my template:
<div class="account">
{% if request.user.is_authenticated %}
<a href="{% url settings %}"
class="iconed username">{{ request.user.username }}</a>
|
<a href="{% url logout %}?next={{ request.path }}"
class="iconed logout">{% trans "Logout" %}</a>
{% else %}
{% trans "Login" %}
|
<a href="{% url sign_up %}?next={{ request.path }}"
class="iconed sign-up">{% trans "Sign up" %}</a>
{% endif %}
</div>
As you can see, it shows different links depends on user logged in or not. It works fine if I test it by hands, but when I try to test it with following code:
def test_home_logged_in(self):
if self.client.login(username='Test', password='secret'):
home = self.app.get('/')
self.assertOK(home)
self.assertContains(home, '/settings/')
self.assertContains(home, '/logout/')
else:
self.fail("Couldn't log in.")
login() returns True, but test fails. I called showbrowser() for home object and see, that page that was returned, looks like page for anonymous user - it contains links to sign up and login besides links to settings and logout.
Is it correct to use *request.user.is_authenticated* in template to check if user is authenticated? Why template doesn't see that user was signed up from test?
Thanks!
Based on your other questions, I'm guessing you're using django_webtest. If so, you can specify to the request which user you want to be logged in as. So to access the homepage as user 'Test' you would do:
home = self.app.get('/', user='Test')
It is correct, but you need to have django.core.context_processors.request in settings.TEMPLATE_CONTEXT_PROCESSORS to make request accessible from templates.