Django表单:我无法从主页访问表单

时间:2018-10-18 21:38:18

标签: django django-models django-forms

当我单击主页上的登录或注册按钮时,它会转到链接,但表格不会出现。页面即将用完。控制台中没有错误出现。当我从login.html删除部分{%扩展“ base_generic.html”%}时,我就成功地访问了登录页面。我找不到我做错的地方。谢谢你的帮助。对不起,我的英语不好。

    #models.py

class MyUser(AbstractUser):
    is_student = models.BooleanField('student status', default=False)
    is_teacher = models.BooleanField('teacher status', default=False)


class Student(models.Model):

    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
    student_no = models.CharField(unique=True, max_length=9, help_text="student number")
    student_name = models.CharField(max_length=50, help_text="student name")
    student_surname = models.CharField(max_length=50, help_text="student surname")

    class Meta:
        permissions = (("can_upload_photo", "Upload Photo Permission"),)

    def __str__(self):
        return self.student_no


# views.py
class StudentSignUpView(CreateView):
    model = MyUser
    form_class = StudentSignUpForm
    template_name = 'registration/signup_form.html'

    def get_context_data(self, **kwargs):
        kwargs['user_type'] = 'student'
        return super().get_context_data(**kwargs)

    def form_valid(self, form):
        user = form.save()
        login(self.request, user)
        return redirect('index.html')


#forms.py
class StudentSignUpForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = MyUser

    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        user.is_student = True
        user.save()
        return user

#urls.py
urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
    path('accounts/signup/', views.SignUpView.as_view(), name='signup'),
    path('accounts/signup/student/', views.StudentSignUpView.as_view(), name='student_signup'),
    path('accounts/signup/teacher/', views.TeacherSignUpView.as_view(), name='teacher_signup'),
]


#base_generic.html
            <div class="col-6 text-right">
              {% if user.is_authenticated %}
                <p class="pt-3">Logged in as <strong>{{ user.username }}</strong>. <a href="{% url 'logout' %}">Log out</a>.</p>
              {% else %}
                <a href="{% url 'login' %}" class="btn btn-light" role="button">Log in</a>
                <a href="{% url 'signup' %}" class="btn btn-primary" role="button">Sign up</a>
              {% endif %}
            </div>


#login.html
{% extends 'base_generic.html' %}

{% block content %}
<div class="row">
    <div class="col-lg-4 col-md-6 col-sm-8 col-12">
      <h2>Log in</h2>
      <form method="post" novalidate>
        {% csrf_token %}
        <input type="hidden" name="next" value="{{ next }}">
        {{ form.username }}
        {{ form.password }}
        <button type="submit" class="btn btn-primary">Log in</button>
      </form>
    </div>
  </div>
{% endblock %}


#signup.html
{% extends 'base_generic.html' %}

{% block content %}
  <h2>Sign up for a free account</h2>
  <p class="lead">Select below the type of account you want to create</p>
  <a href="{% url 'student_signup' %}" class="btn btn-student btn-lg" role="button">I'm a student</a>
  <a href="{% url 'teacher_signup' %}" class="btn btn-teacher btn-lg" role="button">I'm a teacher</a>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

您需要在base_generic.html文件中包含shouldComponentUpdate{% block content %}标签。

“扩展”基本模板时,这些标记指示应在其中插入额外内容。

请参见Template Inheritance