【问题标题】:Clicking the button in the html template does not change the "status" field in the django model单击html模板中的按钮不会更改django模型中的“状态”字段
【发布时间】:2022-12-06 02:27:28
【问题描述】:

每个人!我有 模型.py

class Post(models.Model):
    ...
    status = models.CharField(max_length=16, choices=STATUS_CHOICES, default='Activated')
    ...

网址.py

app_name = 'posts'

urlpatterns = [
    ...
    path('<int:pk>/update/', views.PostUpdateView.as_view(), name='update_status')]

视图.py

class PostUpdateView(UpdateView):
model = Post
template_name = 'post_detail.html'

def change_status(self):
    if request.method == "POST": 
        post = Post.objects.filter(id=self.id)
        if post.status == 'Activated':
            post.status = 'Deactivated'
            post.save()
        elif post.status == 'Deactivated':
            post.status = 'Activated'
            post.save()
    return redirect('posts:post_detail')

posts_detail.html

...
<form action="{% url 'posts:update_status' post.id %}" method="post">
    {% csrf_token %}
    <button type="button">
    {% if post.status == 'Activated' %}
    Deactivate
    {% else %}
    Activate
    {% endif %}</button>
</form>
...

我想切换按钮“激活/停用”上的字段并重定向到同一页面。目前有一个按钮,单击时没有任何变化。好吧,也许重定向有效,但状态不会切换。我假设 views.py 是错误的,但不知道在哪里。

我这样试过

@require_http_methods(['POST'])
def update_status(request, id):
    if post.status == 'Activated':
        Post.objects.filter(id=id).update(status='Deactivated')
    elif post.status == 'Deactivated':
        Post.objects.filter(id=id).update(status='Activated')
    return redirect('posts:post_detail')

但这也不管用。 我知道这些是类似的方式,但我没有更多的想法。

【问题讨论】:

标签: python django button


【解决方案1】:

好吧,也许重定向有效,但状态不会切换。

您的按钮是否正确提交?也许按钮类型='提交'是你需要的。您可以像这样检查按钮是否正常工作。

posts_detail.html

...
{{alert}}
<form action="{% url 'posts:update_status' post.id %}" method="post">
    {% csrf_token %}
    {% if post.status == 'Activated' %}
        <button type="submit">Deactivate</button>
    {% else %}
        <button type="submit">Activate</button>
    {% endif %}
</form>
...

视图.py

def update_status(request, id):
    # For checking submit button works properly
    if self.request.method == "POST":
        context = {'alert':'POST submitted'}
        return render(request, 'templates/posts_detail.html', context)

    context = {'alert':'ready for POST'}
    return render(request, 'templates/posts_detail.html', context)
    ...

如果可行,您可以考虑像这样为按钮添加额外的值以供进一步使用。

posts_detail.html

...
<form action="{% url 'posts:update_status' post.id %}" method="post">
    {% csrf_token %}
    {% if post.status == 'Activated' %}
        <button type="submit" name="status" value="Deactivated">Deactivate</button>
    {% else %}
        <button type="submit" name="status" value="Activated">Activate</button>
    {% endif %}
</form>
...

视图.py

def update_status(request, id):
    if self.request.method == "POST":
        Post.objects.filter(id=id).update(status=request.POST['status'])
    ...

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2010-12-31
    • 2019-10-10
    • 2017-01-26
    • 2021-05-09
    • 1970-01-01
    • 2014-02-01
    • 2012-03-02
    • 2019-12-29
    相关资源
    最近更新 更多