我的用户个人资料更新表单未出现在模板中。我只能看到“更新”按钮和图例名称。但我看不到实际的表格。
这是我的forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth import get_user_model
from .models import Profile, Post, User
# User = get_user_model()
class UserCreationModelForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'country', 'city', 'email', 'password1', 'password2', 'access_challenge']
class UserUpdateForm(forms.ModelForm):
username = forms.CharField()
email = forms.EmailField()
class Meta:
model = User
fields = ['username', 'email' ]
这是我的views.py 我有两种看法。一个是基于分类的视图,另一个是基于功能的视图。
class CabinetView(LoginRequiredMixin, DetailView):
model = User
template_name = 'users/user_detail.html'
def get_object(self):
return self.request.user
def profile(request):
uform = UserUpdateForm()
pform = ProfileUpdateForm()
context = {
'uform': uform,
'pform': pform
}
return render(request, 'users/user_detail.html', context)
这是我的urls.py
from django.urls import path
from .views import UserRegistrationView, CabinetView, PostCreateView, PostDetailView
from . import views
app_name = 'users'
urlpatterns = [
path('accounts/register/', UserRegistrationView.as_view(), name='register'),
path('accounts/cabinet/', CabinetView.as_view(), name='cabinet'),
path('accounts/cabinet/', views.profile, name='profile'),
path('accounts/cabinet/blog/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('accounts/cabinet/new/', PostCreateView.as_view(), name='post-create'),
path('', views.home, name='home'),
path('accounts/cabinet/blog/', views.blog, name='blog'),
path('accounts/cabinet/countries/', views.countries, name='countries'),
path('accounts/cabinet/cities/<int:pk>/', views.cities, name='cities'),
path('accounts/cabinet/address/<int:pk>/', views.address, name='address'),
这是我的user_detail.html模板
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Profile Info</legend>
{{ uform }}
{{ pform }}
</fieldset>
<div class="form-group">
<button class="btn btn-info btn-block" style="border-radius: 0; width: 200px; padding-left: 4px;" type="submit">Update</button>
</div>
</form>
答案 0 :(得分:0)
问题是 CabinetView 类和 profile def指向相同的URL。
因此,您需要将个人资料网址更改为:
path('accounts/cabinet/profile/', views.profile, name='profile'),