Home > Mobile >  Clicking the button in the html template does not change the "status" field in the django
Clicking the button in the html template does not change the "status" field in the django

Time:12-06

everyone! I have models.py

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

urls.py

app_name = 'posts'

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

views.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>
...

I want to switch the field on the button "Activate/Deactivate" and redirect to the same page. At the moment there is a button and when clicked nothing changes. Well or maybe the redirect works, but the status does not switch. I'm assuming wrong views.py, but can't figure out where.

I tried it like this

@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')

But that doesn't work either. I know these are similar ways, but I don't have any more ideas.

CodePudding user response:

Well or maybe the redirect works, but the status does not switch.

Does your button submits properly? Maybe button type='submit' is what you need. You can check does button work properly like this.

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>
...

views.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)
    ...

If it works, you can consider adding extra values to button like this for further use.

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>
...

views.py

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