首页 > 解决方案 > 我的表单没有出现在模板中 {{ form }} 没有做任何事情

问题描述

我在 forms.py 中创建的表单没有显示在模板中。

我在 views.py 中创建了上下文并通过 {{ u_form }} 传递了它,但它仍然不起作用。

我的表格.py

UserCreationModelForm 工作正常。但其他两个不起作用。

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
from .models import Profile

User = get_user_model()

class UserCreationModelForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'country', 'city', 'email', 'password1', 'password2']


class UserUpdateForm(forms.ModelForm):
    username = forms.CharField()
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['username', 'email']


class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

我的意见.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView, DetailView
from .forms import UserCreationModelForm, UserUpdateForm, ProfileUpdateForm
from .models import User

class UserRegistrationView(CreateView):
    form_class = UserCreationModelForm
    success_url = reverse_lazy('login')
    template_name = 'users/registration.html'

class CabinetView(LoginRequiredMixin, DetailView):
    model = User
    def get_object(self):
        return self.request.user


def home(request):
    return render(request, 'registration/home.html')


def profile(request):
    u_form = UserUpdateForm()
    p_form = ProfileUpdateForm()

    context = {

        'u_form': u_form,
        'p_form': p_form
    }

    return render(request, 'registration/user_detail.html', context)

我的主要 urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from apps.users import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('apps.users.urls')),
    path('accounts/cabinet/', views.profile, name='user_detail'),
    path('accounts/', include('django.contrib.auth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的应用程序 urls.py

from django.urls import path
from .views import UserRegistrationView, CabinetView
from . import views

app_name = 'users'

urlpatterns = [
    path('accounts/register/', UserRegistrationView.as_view(), name='register'),
    path('accounts/cabinet/', CabinetView.as_view(), name='cabinet'),
    path('', views.home, name='home'),


]

我的模型.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from PIL import Image


class User(AbstractUser):
    first_name = models.CharField(verbose_name="First name", max_length=255)
    last_name = models.CharField(verbose_name="First name", max_length=255)
    country = models.CharField(verbose_name="Country name", max_length=255)
    city = models.CharField(verbose_name="City name", max_length=255)
    email = models.EmailField(verbose_name="Email", max_length=255)

    def __str__(self):
        return self.username

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

我想要表格的模板:

{% extends 'shared/base.html' %}
{% load staticfiles %}
{% load crispy_forms_tags %}


{% block content %}

<br>
<br>
<div class="container mt-5">
<div class="row">



<div class="col-sm-6 " style="background-color: #eee;">
  <div class="media">
    <img class="rounded-circle pt-4" style="width: 200px;" src="{{ user.profile.image.url }}">
    <div class="media-body pl-4 pt-4">
      <h4 class="account-heading"> username: {{ object.username }}   </h4>
      <p class="text-secondary"> First name: {{ object.first_name }} </p>
      <p class="text-secondary"> Last name: {{ object.last_name }}   </p>
      <p class="text-secondary"> Email: {{ object.email }}           </p>
      <p class="text-secondary"> Country: {{ object.country }}       </p>
      <p class="text-secondary"> City: {{ object.city }}             </p>
    </div>
  </div>

  <div class="col-sm-6 mt-5 pt-5">
    <form method="post">
      {% csrf_token %}
      <fieldset class="form-group pr-4">
        <legend class="mb-4">Join Today</legend>
          {{ u_form|crispy }}
          {{ p_form|crispy }}

      </fieldset>
      <div class="form-group">
        <button style="border-radius: 0; width: 200px; padding-left: 4px;" class="btn btn-info btn-block" type="submit">Sign Up</button>
      </div>
    </form>

  </div>
  </div>

  <div class="col-sm-6" style="background-color: white;">
    <div class="media">
      <div class="media-body pl-4" style="background-color: white; width: 500px; padding-left: 14px; margin-right: 1px;">
        <h4 class="account-heading"> {{ object.username }} posts.  </h4>
          <table class="table table-hover">
            <thead>
              <tr>
              <th scope="col">No:</th>
              <th scope="col">Name:</th>
              <th scope="col">Country:</th>
              <th scope="col">City:</th>
              <th scope="col">Address:</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">1</th>
              <td>Mark</td>
              <td>Otto</td>
              <td>@mdo</td>
              <td>@mdo</td>
            </tr>
          </tbody>
        </table>
    </div>
</div>

</div>
</div>

{% endblock %}

我希望能够更新个人资料和更新图像。

标签: django

解决方案


推荐阅读