/ / Logowanie do Django w oknie modalnym - django, django-formularze, django-templates, django-views, django-crispy-forms

Django zaloguj się w oknie modalnym - django, django-forms, django-templates, django-views, django-crispy-forms

Mam formularz logowania i chcę umieścić okno modalne w nagłówku.

Urls.py

url(r"^account/login/$", appuser_views.LoginView.as_view(template_name = "account/login/index.html", form_class = appuser_forms.LoginForm, target_url = LOGIN_TARGET_URL)),

views.py

class LoginView(ResendEmailToUsersMixin, AjaxFormView):

def process_form(self, request, form):
data = form.cleaned_data

a = AppUserCredential.objects.select_related("appuser").filter(
data1 = data["email_address"],
credential_type = AppUserCredential.CREDENTIAL_TYPES.EMAIL_PASSWORD,
appuser__status = AppUser.STATUS_TYPES.Active
).first()

force_error = False
if a is None:
response = self.resend_email_to_users(data = data)
if response is not None:
return response
#If no email exists force the error and don"t check the password
force_error = True

if force_error or not a.check_password(data["password"]):
return AjaxErrorResponse(code="login_error", title="Username or Password Error", message="The username or password you have provided don’t match our records. Please check your entries and try again.")

a.appuser.login(request)

forms.py

class LoginForm(AjaxForm):
email_address = forms.EmailField(label = "Email Address", required = True, max_length = 100,
widget = forms.TextInput(attrs = {"placeholder": "email@domain.com", "autocomplete":"off"}))
password = forms.CharField(label = "Password", required = True, max_length = 100,
widget = forms.PasswordInput(attrs = {"placeholder": "Password", "autocomplete":"off"}))

def setup_form_helper(self, helper):
helper.form_id = "login_form"
helper.layout = Layout(
"email_address",
"password",
Div(
Submit("submit", "Login", css_class="btn btn-primary"),
css_class="form-group text-center"
),
HTML("<p class="pull-right light-top-bottom-padding"><a href="/account/forgot-password" title="Forgot Password">Forgot Password?</a></p>")
)

/templates/account/login/index.html

...
{% crispy form form.helper %}
...

Stworzyłem okno modalne w /templates/layouts/header.html Jak mogę umieścić {% crispy form form.helper %} w oknie modalnym? Dzięki.

UPD1: Jeśli to zrobię {% crispy form form.helper %} w header.html Otrzymałem błąd

VariableDoesNotExist at / Failed lookup for key [form] in u "[{" False ": False," None ": None," True ": True},

UPD2: Forma modalna:

    <a href="javascript:void(0);" class="text-btn" data-toggle="modal" data-target="#login">login</a>

<div id="login" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
<div class="panel-body">
<header class="section-title text-center normal-top-bottom-padding">
<h1>Login</h1>
</header>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>

</div>
</div>

Link do logowania powinien znajdować się na każdej stronie.

Odpowiedzi:

3 dla odpowiedzi № 1

Jeśli chcesz wyświetlać formularz na wszystkich stronach, musisz dodać rozszerzenie form (lepiej to nazwij login_form więc nie koliduje z innymi formami, które możesz mieć) w każdym kontekście Widoku.

Aby uniknąć powtarzania tego we wszystkich widokach, Django ma procesory kontekstu. Włączasz je settings.py"s TEMPLATES zmienna. Przykład

TEMPLATES = [{
...
"OPTIONS": {
"context_processors": [
...
"myapp.context_processors.add_my_login_form",
],
}]

Następnie utwórz plik o nazwie context_processors.py i dodaj add_my_login_form() tam funkcjonować. Funkcja zwraca login_form do kontekstu wszystkich żądań.

def add_my_login_form(request):
return {
"login_form": LoginForm(),
}

Ponieważ renderujesz formularz na każdej stronie, może warto skorzystać z buforowania szablonów.