马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想实现用户修改文章, 但是有一块内容获得不了
参考修改用户信息做的:@login_required(login_url='users:login') # 登录之后允许访问
def editor_users(request):
""" 编辑用户信息 """
user = User.objects.get(id=request.user.id)
if request.method == "POST":
try:
userprofile = user.userprofile
form = UserForm(request.POST, instance=user)
user_profile_form = UserProfileForm(request.POST, request.FILES, instance=userprofile) # 向表单填充默认数据
if form.is_valid() and user_profile_form.is_valid():
form.save()
user_profile_form.save()
return redirect('users:user_profile')
except UserProfile.DoesNotExist: # 这里发生错误说明userprofile无数据
form = UserForm(request.POST, instance=user) # 填充默认数据 当前用户
user_profile_form = UserProfileForm(request.POST, request.FILES) # 空表单,直接获取空表单的数据保存
if form.is_valid() and user_profile_form.is_valid():
form.save()
# commit=False 先不保存,先把数据放在内存中,然后再重新给指定的字段赋值添加进去,提交保存新的数据
new_user_profile = user_profile_form.save(commit=False)
new_user_profile.owner = request.user
new_user_profile.save()
return redirect('users:user_profile')
else:
try:
userprofile = user.userprofile
form = UserForm(instance=user)
user_profile_form = UserProfileForm(instance=userprofile)
except UserProfile.DoesNotExist:
form = UserForm(instance=user)
user_profile_form = UserProfileForm() # 显示空表单
return render(request, 'users/editor_users.html', locals())
{% extends 'users/user_profile.html' %}
{% load static %}
{% block title %}
<li class="is-active"><a>修改用户信息</a></li>
{% endblock title %}
{% block content %}
<div class="content">
<fieldset disabled style="margin-bottom: 1em">
<div class="field is-horizontal">
<div class="field-label is-normal">
<label class="label">用户名</label>
</div>
<div class="field-body">
<div class="field">
<p class="control is-expanded has-icons-left">
<input class="input" type="text" placeholder="Name" value="{{ user.username }}">
<span class="icon is-small is-left">
<i class="fas fa-user"></i>
</span>
</p>
</div>
</div>
</div>
<div class="field is-horizontal">
<div class="field-label is-normal">
<label for="{{ user.email.id_for_label }}" class="label">邮箱</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
{{ user.email }}
</div>
</div>
</div>
</div>
</fieldset>
<form action="{% url 'users:editor_users' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in user_profile_form %}
<div class="field is-horizontal">
<div class="field-label is-normal">
<label for="{{ field.id_for_label }}" class="label">{{ field.label }}</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
{{ field }}
</div>
</div>
</div>
</div>
{% endfor %}
<input class=" button is-primary is-pulled-right" type="submit" value="提交修改">
<div class=" is-clearfix"></div>
</form>
</div>
{% endblock content %}
这是我的修改文章的逻辑:@login_required(login_url='users:login') # 登录之后允许访问
def post_change(request, post_id):
# 文章详情页
user = User.objects.get(id=request.user.id)
post_info = get_object_or_404(Post, id=post_id)
# print(type(post_info),)
if request.method == "POST":
post_form = PostForm(request.POST, request.FILES, instance=post_info) # 向表单填充默认数据
if post_form.is_valid():
post_form.save()
return redirect('users:post_manage')
else:
post_form = PostForm(request.POST, request.FILES, instance=post_info) # 向表单填充默认数据
print(post_form, '='*100)
context = {'post': post_form}
return render(request, 'users/post_change.html', context)
{% extends 'users/user_profile.html' %}
{% load static %}
{% block title %}
<li class="is-active"><a>修改文章</a></li>
{% endblock title %}
{% block content %}
<div class="content">
<form action="{% url 'users:editor_users' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in post_form %}
<div class="field is-horizontal">
<div class="field-label is-normal">
<label for="{{ field.id_for_label }}" class="label">{{ field.label }}</label>
</div>
<div class="field-body">
<div class="field">
<div class="control">
{{ field }}
</div>
</div>
</div>
</div>
{% endfor %}
<input class=" button is-primary is-pulled-right" type="submit" value="提交修改">
<div class=" is-clearfix"></div>
</form>
</div>
{% endblock content %}
这是 print(post_form) 的结果:<tr>
<th><label for="id_title">文章标题:</label></th>
<td>
<ul class="errorlist"><li>这个字段是必填项。</li></ul>
<input type="text" name="title" maxlength="61" required id="id_title">
</td>
</tr>
<tr>
<th><label for="id_desc">文章描述:</label></th>
<td>
<textarea name="desc" cols="40" rows="10" maxlength="200" id="id_desc">
</textarea>
</td>
</tr>
<tr>
<th><label for="id_category">分类:</label></th>
<td>
<ul class="errorlist"><li>这个字段是必填项。</li></ul>
<select name="category" required id="id_category">
<option value="" selected>---------</option>
<option value="1">这是分类1</option>
<option value="2">这是分类2</option>
</select>
</td>
</tr>
<tr>
<th><label for="id_content">文章详情:</label></th>
<td>
<ul class="errorlist"><li>这个字段是必填项。</li></ul>
<textarea name="content" cols="40" rows="10" required id="id_content">
</textarea>
</td>
</tr>
<tr>
<th><label for="id_tags">文章标签:</label></th>
<td>
<select name="tags" id="id_tags">
<option value="" selected>---------</option>
<option value="1">这是标签</option>
</select>
</td>
</tr>
啥都没拿到!
本来应该有这篇文章的各种信息的
求助哪儿出了问题?
本帖最后由 阿奇_o 于 2022-4-7 13:46 编辑
这种只能自己一步步确认咯,比如
- post的数据,后端是否正确地拿到了?
- 相应的文章内容,即 数据(数据库)是否正确地 被修改了?
- views视图函数,是否能正确读取到已修改的数据?
- view --> tempalte , 视图函数传给html模板的数据,能否正确显示出来?
…… Good Luck.
|