鱼C论坛

 找回密码
 立即注册
查看: 1603|回复: 2

[已解决]python django 博客 用户怎么修改文章?

[复制链接]
发表于 2022-4-7 12:37:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想实现用户修改文章, 但是有一块内容获得不了

参考修改用户信息做的:
  1. @login_required(login_url='users:login')   # 登录之后允许访问
  2. def editor_users(request):
  3.     """ 编辑用户信息 """
  4.     user = User.objects.get(id=request.user.id)
  5.     if request.method == "POST":
  6.         try:
  7.             userprofile = user.userprofile
  8.             form = UserForm(request.POST, instance=user)
  9.             user_profile_form = UserProfileForm(request.POST, request.FILES, instance=userprofile)  # 向表单填充默认数据
  10.             if form.is_valid() and user_profile_form.is_valid():
  11.                 form.save()
  12.                 user_profile_form.save()
  13.                 return redirect('users:user_profile')
  14.         except UserProfile.DoesNotExist:   # 这里发生错误说明userprofile无数据
  15.             form = UserForm(request.POST, instance=user)       # 填充默认数据 当前用户
  16.             user_profile_form = UserProfileForm(request.POST, request.FILES)  # 空表单,直接获取空表单的数据保存
  17.             if form.is_valid() and user_profile_form.is_valid():
  18.                 form.save()
  19.                 # commit=False 先不保存,先把数据放在内存中,然后再重新给指定的字段赋值添加进去,提交保存新的数据
  20.                 new_user_profile = user_profile_form.save(commit=False)
  21.                 new_user_profile.owner = request.user
  22.                 new_user_profile.save()

  23.                 return redirect('users:user_profile')
  24.     else:
  25.         try:
  26.             userprofile = user.userprofile
  27.             form = UserForm(instance=user)
  28.             user_profile_form = UserProfileForm(instance=userprofile)
  29.         except UserProfile.DoesNotExist:
  30.             form = UserForm(instance=user)
  31.             user_profile_form = UserProfileForm()  # 显示空表单
  32.     return render(request, 'users/editor_users.html', locals())
复制代码

  1. {% extends 'users/user_profile.html' %}
  2. {% load static %}
  3. {% block title %}
  4. <li class="is-active"><a>修改用户信息</a></li>
  5. {% endblock title %}

  6. {% block content %}
  7. <div class="content">
  8.     <fieldset disabled style="margin-bottom: 1em">
  9.         <div class="field is-horizontal">
  10.           <div class="field-label is-normal">
  11.             <label class="label">用户名</label>
  12.           </div>
  13.           <div class="field-body">
  14.             <div class="field">
  15.               <p class="control is-expanded has-icons-left">
  16.                 <input class="input" type="text" placeholder="Name" value="{{ user.username }}">
  17.                 <span class="icon is-small is-left">
  18.                   <i class="fas fa-user"></i>
  19.                 </span>
  20.               </p>
  21.             </div>
  22.           </div>  
  23.         </div>
  24.         <div class="field is-horizontal">
  25.           <div class="field-label is-normal">
  26.             <label for="{{ user.email.id_for_label }}" class="label">邮箱</label>
  27.           </div>
  28.           <div class="field-body">
  29.             <div class="field">
  30.               <div class="control">
  31.                   {{ user.email }}
  32.               </div>
  33.             </div>
  34.           </div>
  35.       </div>
  36.     </fieldset>
  37.   
  38.    
  39.     <form action="{% url 'users:editor_users' %}" method="post" enctype="multipart/form-data">
  40.       {% csrf_token %}
  41.       
  42.         {% for field in user_profile_form %}
  43.         <div class="field is-horizontal">
  44.             <div class="field-label is-normal">
  45.               <label for="{{ field.id_for_label }}" class="label">{{ field.label }}</label>
  46.             </div>
  47.             <div class="field-body">
  48.               <div class="field">
  49.                 <div class="control">
  50.                    {{ field }}
  51.                 </div>
  52.               </div>
  53.             </div>
  54.         </div>
  55.         {% endfor %}
  56.         <input class=" button is-primary is-pulled-right" type="submit" value="提交修改">
  57.         <div class=" is-clearfix"></div>
  58.     </form>
  59. </div>
  60. {% endblock content %}
复制代码


这是我的修改文章的逻辑:
  1. @login_required(login_url='users:login')   # 登录之后允许访问
  2. def post_change(request, post_id):
  3.     # 文章详情页
  4.     user = User.objects.get(id=request.user.id)
  5.     post_info = get_object_or_404(Post, id=post_id)
  6.     # print(type(post_info),)
  7.     if request.method == "POST":
  8.         post_form = PostForm(request.POST, request.FILES, instance=post_info)  # 向表单填充默认数据
  9.         if post_form.is_valid():
  10.             post_form.save()
  11.             return redirect('users:post_manage')

  12.     else:
  13.         post_form = PostForm(request.POST, request.FILES, instance=post_info)  # 向表单填充默认数据
  14.         print(post_form, '='*100)
  15.     context = {'post': post_form}
  16.     return render(request, 'users/post_change.html', context)
复制代码

  1. {% extends 'users/user_profile.html' %}
  2. {% load static %}
  3. {% block title %}
  4. <li class="is-active"><a>修改文章</a></li>
  5. {% endblock title %}

  6. {% block content %}
  7. <div class="content">
  8.     <form action="{% url 'users:editor_users' %}" method="post" enctype="multipart/form-data">
  9.       {% csrf_token %}
  10.       
  11.         {% for field in post_form %}
  12.         <div class="field is-horizontal">
  13.             <div class="field-label is-normal">
  14.               <label for="{{ field.id_for_label }}" class="label">{{ field.label }}</label>
  15.             </div>
  16.             <div class="field-body">
  17.               <div class="field">
  18.                 <div class="control">
  19.                    {{ field }}
  20.                 </div>
  21.               </div>
  22.             </div>
  23.         </div>
  24.         {% endfor %}
  25.         <input class=" button is-primary is-pulled-right" type="submit" value="提交修改">
  26.         <div class=" is-clearfix"></div>
  27.     </form>
  28. </div>
  29. {% endblock content %}
复制代码


这是 print(post_form) 的结果:
  1. <tr>
  2.     <th><label for="id_title">文章标题:</label></th>
  3.     <td>
  4.       <ul class="errorlist"><li>这个字段是必填项。</li></ul>
  5.       <input type="text" name="title" maxlength="61" required id="id_title">


  6.     </td>
  7.   </tr>

  8.   <tr>
  9.     <th><label for="id_desc">文章描述:</label></th>
  10.     <td>

  11.       <textarea name="desc" cols="40" rows="10" maxlength="200" id="id_desc">
  12. </textarea>


  13.     </td>
  14.   </tr>

  15.   <tr>
  16.     <th><label for="id_category">分类:</label></th>
  17.     <td>
  18.       <ul class="errorlist"><li>这个字段是必填项。</li></ul>
  19.       <select name="category" required id="id_category">
  20.   <option value="" selected>---------</option>

  21.   <option value="1">这是分类1</option>

  22.   <option value="2">这是分类2</option>

  23. </select>


  24.     </td>
  25.   </tr>

  26.   <tr>
  27.     <th><label for="id_content">文章详情:</label></th>
  28.     <td>
  29.       <ul class="errorlist"><li>这个字段是必填项。</li></ul>
  30.       <textarea name="content" cols="40" rows="10" required id="id_content">
  31. </textarea>


  32.     </td>
  33.   </tr>

  34.   <tr>
  35.     <th><label for="id_tags">文章标签:</label></th>
  36.     <td>

  37.       <select name="tags" id="id_tags">
  38.   <option value="" selected>---------</option>

  39.   <option value="1">这是标签</option>

  40. </select>




  41.     </td>
  42.   </tr>
复制代码


啥都没拿到!

本来应该有这篇文章的各种信息的

求助哪儿出了问题?
最佳答案
2022-4-7 13:45:16
本帖最后由 阿奇_o 于 2022-4-7 13:46 编辑

这种只能自己一步步确认咯,比如
  - post的数据,后端是否正确地拿到了?
  - 相应的文章内容,即 数据(数据库)是否正确地 被修改了?
  - views视图函数,是否能正确读取到已修改的数据?
  - view --> tempalte , 视图函数传给html模板的数据,能否正确显示出来?

…… Good Luck.

本帖被以下淘专辑推荐:

  • · django|主题: 61, 订阅: 0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-7 13:45:16 | 显示全部楼层    本楼为最佳答案   
本帖最后由 阿奇_o 于 2022-4-7 13:46 编辑

这种只能自己一步步确认咯,比如
  - post的数据,后端是否正确地拿到了?
  - 相应的文章内容,即 数据(数据库)是否正确地 被修改了?
  - views视图函数,是否能正确读取到已修改的数据?
  - view --> tempalte , 视图函数传给html模板的数据,能否正确显示出来?

…… Good Luck.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-7 23:47:47 | 显示全部楼层
阿奇_o 发表于 2022-4-7 13:45
这种只能自己一步步确认咯,比如
  - post的数据,后端是否正确地拿到了?
  - 相应的文章内容,即 数据 ...

哈哈, 还是谢了,问题解决了
返回的数据类型不对
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表