首页 > 解决方案 > 如何在 django 视图中使用注册(注册/登录)模式(引导模板)?

问题描述

我是编程新手。我找到了很好的注册引导模式,所以我把它放在我的 html 代码中,它看起来不错,但什么都不能按下或发布。在我使用没有模式的 django 和 UserCreationForm 之前。所以帮助我连接这两件事:

所以这是我发现的引导模式

<!-- Modal -->
<div class="modal fade" id="elegantModalForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
  aria-hidden="true">
  <div class="modal-dialog" role="document">
    <!--Content-->
    <div class="modal-content form-elegant">
      <!--Header-->
      <div class="modal-header text-center">
        <h3 class="modal-title w-100 dark-grey-text font-weight-bold my-3" id="myModalLabel"><strong>Войти</strong></h3>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <!--Body-->
        <form type="post" action="{% url 'common:login' %}">
      <div class="modal-body mx-4">
        <!--Body-->
        <div class="md-form mb-5">
          <input type="email" name="useremail" id="Form-email1" class="form-control validate">
          <label data-error="wrong" data-success="right" for="Form-email1">Ваша почта</label>
        </div>

        <div class="md-form pb-3">
          <input type="password" id="Form-pass1" class="form-control validate">
          <label data-error="wrong" data-success="right" for="Form-pass1">Пароль</label>
          <p class="font-small blue-text d-flex justify-content-end">Забыли <a href="#" class="blue-text ml-1">
              пароль?</a></p>
        </div>

        <div class="text-center mb-3">
          <button type="button" class="btn blue-gradient btn-block btn-rounded">ок</button>
        </div>
        <p class="font-small dark-grey-text text-right d-flex justify-content-center mb-3 pt-2"> или войти
          с помощью:</p>

        <div class="row my-3 d-flex justify-content-center">
          <!--Facebook-->
          <button type="button" class="btn btn-white btn-rounded mr-md-3 z-depth-1a"><i class="fab fa-facebook-f text-center"></i></button>
          <!--Twitter-->
          <button type="button" class="btn btn-white btn-rounded mr-md-3 z-depth-1a"><i class="fab fa-twitter"></i></button>
          <!--Google +-->
          <button type="button" class="btn btn-white btn-rounded z-depth-1a"><i class="fab fa-google-plus-g"></i></button>
        </div>
      </div>
            </form>
      <!--Footer-->
      <div class="modal-footer mx-5 pt-3 mb-1">
        <p class="font-small grey-text d-flex justify-content-end">Первый раз? <a href="{% url 'common:signup' %}" class="blue-text ml-1">
            Зарегистрируйся</a></p>
      </div>
    </div>
    <!--/.Content-->
  </div>
</div>
<!-- Modal -->

这是我在使用模态之前的views.py:

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('index')
    else:
        form = UserCreationForm()
    return render(request, 'registration/signup.html', {'form': form})

所以帮助我展示它应该如何工作。实际上我曾经使用 {{form.as_p}} 但在这种情况下我应该怎么做?

标签: pythonhtmldjango

解决方案


我认为您正在寻找的是 Django Forms

官方文档- https://docs.djangoproject.com/en/2.2/topics/forms/

其他-(https://www.tutorialspoint.com/django/django_form_processing

首先,您需要创建一个表单模型并定义您作为输入的字段。这是一个示例-

from django import forms

class LoginForm(forms.Form):
  user = forms.CharField(max_length = 100)
  password = forms.CharField(max_length =100)

然后,您需要根据表单修改视图。这是一个示例-

def login(request):
  username = "not logged in"
  if request.method == "POST":
    #Get the posted form
    MyLoginForm = LoginForm(request.POST)

    if MyLoginForm.is_valid():
      username = MyLoginForm.cleaned_data['username']
  else:
    MyLoginForm = Loginform()

  return render(request, 'loggedin.html', {"username" : username})

如果您通过 POST 发送数据,请不要忘记在 Html 文件中添加 csrf 令牌。

form type="post" action="{% url 'common:login' %}"> {% csrf_token %}


推荐阅读