Django-在post请求后重新加载模板

Django-在post请求后重新加载模板,django,Django,点击post请求按钮后,我的模板不会立即重新加载。但是我知道这个请求工作得很好,因为如果我手动加载页面(带有链接),我可以看到它被正确地更改了。有什么想法吗?我知道问题在于post方法的渲染 def配置文件(请求,用户): 此用户=request.user profileuser=get\u object\u或\u 404(用户,用户名=User) posts=Post.objects.filter(user=profileuser).order_by('id').reverse() follo

点击post请求按钮后,我的模板不会立即重新加载。但是我知道这个请求工作得很好,因为如果我手动加载页面(带有链接),我可以看到它被正确地更改了。有什么想法吗?我知道问题在于post方法的渲染

def配置文件(请求,用户):
此用户=request.user
profileuser=get\u object\u或\u 404(用户,用户名=User)
posts=Post.objects.filter(user=profileuser).order_by('id').reverse()
followed=Follow.objects.filter(followed=profileuser)
following=Follow.objects.filter(following=profileuser)
所有后续=len(后续)
所有后续=len(后续)
paginator=paginator(职位,10)
如果请求。获取。获取(“页面”)!=无:
尝试:
posts=paginator.page(request.GET.GET(“page”))
除:
posts=分页器。第(1)页
其他:
posts=分页器。第(1)页
尝试:
两个\u follow=follow.objects.filter(follow=profileuser,follow=request.user)
除:
两个_follow==False
上下文={
“职位”:职位,
“profileuser”:profileuser,
“所有跟随”:所有跟随,
“所有跟随”:所有跟随,
“两个跟随”:两个跟随,
“此用户”:此用户,
}
如果request.method==“GET”:
返回呈现(请求“network/profile.html”,上下文)
如果request.method==“POST”:
如果两者都遵循:
两个_follow.delete()
两个_follow==False
其他:
f=跟随()
f、 跟踪=配置文件用户
f、 following=request.user
f、 保存()
两个_follow==True
返回呈现(请求“network/profile.html”,上下文)
html是:

<p>
    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#followed">{{ all_followed }} Followers </button>
    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#following">{{ all_following }} Following </button>
</p>

{% if profileuser != this_user %}

    {% if both_follow %}
        <form action="{% url 'profile' profileuser %}" method="POST">   {% csrf_token %}
             <button type="submit" class="btn btn-primary">UnFollow</button>
        </form>
        
    {% else %}
        <form action="{% url 'profile' profileuser %}" method="POST">{% csrf_token %}
             <button type="submit" class="btn btn-primary">Follow</button>
        </form>
    {% endif %}

{% endif %}

{{all_follow}}追随者
{{all_following}}following

{%if profileuser!=此用户%} {%如果两个_都跟随%} {%csrf_令牌%} 展开 {%else%} {%csrf_令牌%} 跟随 {%endif%} {%endif%}
如果request.method==“GET”,则不需要处理
,如果您没有执行
POST
请求,则默认情况下是
GET

此外,最好在
发布后执行
重定向
,而不使用包含结果的呈现页面进行回复,因为可以通过以下方式重新提交
发布
请求(这可能导致数据重复):

  • 使用刷新/重新加载浏览器按钮重新加载结果页面(显式页面重新加载,隐式重新提交请求)
  • 单击后退和前进浏览器按钮(隐式页面重新加载和隐式重新提交请求)
  • 提交后返回HTML表单,再次单击表单上的提交按钮(显式重新提交请求)
因此,您可以在点击
POST
后重定向到页面

现在我不知道您是如何为
profile
视图命名url模式的,但假定您的url.py是:

urlpatterns=[
...,
路径('network/profile/',views.profile,name='network\u profile')
]
您的视图.py将如下所示:

从django.shortcuts导入重定向,反向
def配置文件(请求、用户):
此用户=request.user
profileuser=get\u object\u或\u 404(用户,用户名=User)
posts=Post.objects.filter(user=profileuser).order_by('id').reverse()
followed=Follow.objects.filter(followed=profileuser)
following=Follow.objects.filter(following=profileuser)
所有后续=len(后续)
所有后续=len(后续)
paginator=paginator(职位,10)
如果请求。获取。获取(“页面”)!=无:
尝试:
posts=paginator.page(request.GET.GET(“page”))
除:
posts=分页器。第(1)页
其他:
posts=分页器。第(1)页
尝试:
两个\u follow=follow.objects.filter(follow=profileuser,follow=request.user)
除:
两个_follow==False
上下文={
“职位”:职位,
“profileuser”:profileuser,
“所有跟随”:所有跟随,
“所有跟随”:所有跟随,
“两个跟随”:两个跟随,
“此用户”:此用户,
}
如果request.method==“POST”:
如果两者都遵循:
两个_follow.delete()
两个_follow==False
其他:
f=跟随()
f、 跟踪=配置文件用户
f、 following=request.user
f、 保存()
两个_follow==True
返回重定向(反向('network_profile'))#我们重定向到同一个视图
返回呈现(请求“network/profile.html”,上下文)