django - How to move singup\signin templates into dropdown menu? -


i have fixed navigation , want add dropdown box users can singup\in (as twitter uses).

i tried:

# project/tempates/signup.html {% load i18n %} {% load account socialaccount %}  {% block head_title %}{% trans "signup" %}{% endblock %}  {% block content %}    <h1>{% trans "sign up" %}</h1>    <p>{% blocktrans %}already have account? please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p>    <form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">     {% csrf_token %}     {{ signupform.as_p }}     {% if redirect_field_value %}     <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />     {% endif %}     <button type="submit">{% trans "sign up" %} &raquo;</button>   </form>  {% endblock %}  # project/tempates/base.html # ... lot of basic stuff <li class="dropdown">     <a class="dropdown-toggle" href="#" data-toggle="dropdown">sign in <strong class="caret"></strong></a>     <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">         {% include './signup.html' %} # ... rest stuff 

and in dropdown box see text, link signin, , button confirmation of registration.

there no fields enter email , passwords. understand, because no access form, views' jobs. how can workable dropdown forms?

after 2 days of internet digging want summarize found.

there few ways:

1. use <form action='some address here'>. easiest way.

to check default allauth forms need to:

# ./manage.py shell >>> import allauth.account.forms forms >>> f = forms.loginform() >>> print(f) 

below edited version of print(f) added directly base.html

<form action="{% url 'account_login' %}" method="post">     {% csrf_token %}     <input type="hidden" name="next" value="{{ request.get_full_path }}" />      <input id="id_login" name="login" placeholder="username or e-mail" type="text" required />     <input id="id_password" name="password" placeholder="password" type="password" required />      <label for="id_remember">remember me:</label>     <input id="id_remember" name="remember" type="checkbox" />      <button type="submit">login</button>     <a href="{% url 'account_reset_password' %}">forgot password?</a> </form> 

method based on solution ->here<-

2. contex processor

a) make folder your_project/your_app/context_processor. put there 2 files - __init__.py , login_ctx.py

b) in login_ctx.py add:

from allauth.account.forms import loginform  def login_ctx_tag(request):     return {'loginctx': loginform()} 

c) in project's settings add your_app.context_processors.login_ctx.login_form_ctx' intemplates` section. like:

templates = [     {         'backend': 'django.template.backends.django.djangotemplates',         'dirs': [os.path.join(base_dir, 'templates'), os.path.join(base_dir, 'templates', 'allauth')],         'app_dirs': true,         'options': {             'debug': debug,             'context_processors': [                 'your_app.context_processors.login_ctx.login_form_ctx',  # <- put processor here                  'django.template.context_processors.debug',                 # [...other processors...]             ],         },     }, ] 

d) in *.html need add next:

{% if not user.is_authenticated %}     <form action="{% url 'account_login' %}" method="post">         {% csrf_token %}         <input type="hidden" name="next" value="{{ request.get_full_path }}" />         {{ loginctx }}         <button type="submit">login</button>     </form> {% else %}     {# display else here... (username?) #} {% endif %} 

3. template tag

a) make folder your_project/your_app/templatetags. put there 2 files - __init__.py , login_tag.py

b) in login_tag.py add:

from django import template allauth.account.forms import loginform  register = template.library()  @register.inclusion_tag('profiles/true_login.html') def login_form_tag(current_page=none):     return {'loginform': loginform(),             'redirect_to': current_page} 

c) in your_project/your_app/templates/your_app/ make file login_form.html content:

{% load account  %}  {% if not user.is_authenticated %}     <form action="{% url 'account_login' %}" method="post">         {% csrf_token %}         <input type="hidden" name="next" value="{{ redirect_to }}" />         {{ loginform }}         <button type="submit">login</button>     </form> {% else %}     {# display else here... (username?) #} {% endif %} 

d) in *.html need, add @ top {% load login_tag %} , in needed place add {% login_form_tag request.get_full_path %}


the 2nd , 3rd methods show native allauth form. if need edit somehow using {{form}}, ->here<- in doc can find examples how that. want mention, if in doc shown like:

<div class="fieldwrapper">     {{ form.subject.errors }}     {{ form.subject.label_tag }}     {{ form.subject }} </div> 

for our case form must changed loginctx or loginform

also can write own form or inherit allauth , import context processor or templatetag shown above.

both methods based on ->this solution<-

in 3 methods redirect works needed (return user previous page, in case of success login, else redirect original allauth template @ site.com/account/login).

all written above can implemented signup.

also asked people, how show errors in case of wrong username\password instead of redirect site.com/account/login, proposition use ajax, out of knowledge. base info connection signin\up forms default allauth views can found ->here<-. if implement it, or find tutorial, please post here.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -