简体   繁体   中英

Django button redirection errors

My webpage should display a series of posts(texts) which I call "views". I want the user to be able to click the "update_button" for that view (called v) and be directed to that page. I have included a link below which should do exactly the same thing as the button. The link redirects to the correct page whereas even though the button clearly has the same action as the link it redirects to a totally different page. This behavior is extremely bizarre. If I replace v.pk in the form's action with, let's say, 9 then it will correctly redirect me to the post with primary key 9. However if I replace the hardcoded 9 with v.pk again, and click the button (for the post with pk=9) it will redirect me to the post with pk=7 (the lowest pk of a post that this user can in fact update), but this is the wrong pk, when I place a print(pk) in the view it prints 7 and not 9, so the value being placed in the link is not actually v.pk. To make matters weirder, when I inspect element on the button, I see the CORRECT link. If I cut and paste this link into the address bar I will be redirected to the correct page. It's like the button is broken whilst displaying correctly with inspect element.

{% extends "uv/base.html" %}

{% block content %}
    <h1>View List</h1>

    {% if view_c_list %}
    <ul>

      {% for v in view_c_list %}
      <li>
        Title: <a href = "{% url 'uv:view_C_info' v.pk %}">{{v.title}}</a></br>
        Author: {{v.author}} </br>
        Piece: {{v.view_text}}
        {% if user == v.author %}
        <form action="{% url 'uv:view_C_delete' v.pk %}" method="POST">
            {% csrf_token %}            
            <input type="submit" id="delete_button" value="delete">
        </form>
        <form action="{% url 'uv:view_C_update' v.pk %}" id="update">
            {% csrf_token %}
            <input type="submit" id="update_button" value="update_{{v.pk}}" form="update">
        </form>
        <a href = "{% url 'uv:view_C_update' v.pk %}"> Update </a>
        {% endif %}
      </li>
      {% endfor %}    
    </ul>
    {% else %}
      <p>That's all folks.</p>
    {% endif %}       
{% endblock %}

urls.py

    from django.urls import path
    from django.conf.urls import url
    from django.contrib import admin
    from django.contrib.auth import views as auth_views
    from . import views
    app_name = 'uv'
    urlpatterns = [
        path('view_leaderboards/',views.view_leaderboards,name='view_leaderboards'),
        path('view/<int:pk>/add_review/',views.add_review,name="add_review"),
        path('view/<int:pk>/update_substars/',views.update_substars,name='update_substars'),
        path('profile/',views.home, name = 'home'),
        path('login/', auth_views.login, name='login'),
        path('logout/', auth_views.logout,{'next_page': '/uv/home/'},name='logout'),
        path('admin/',admin.site.urls),
        path('signup/', views.signup, name='signup'),
        path('home/', views.home, name='home'),
        path('create/', views.View_C_Create.as_view(), name='view_c_create'),
        #path('view/<int:pk>/update/', views.View_C_Update.as_view(), name = 'view_C_update'),
        path('view/<int:pk>/update/', views.view_C_Update, name = 'view_C_update'),
        path('view/<int:pk>/delete/', views.view_C_Delete, name='view_C_delete'),
        path('review/<int:pk>/update/', views.Review_Update.as_view(), name = 'review_update'),
        path('review/<int:pk>/delete/', views.Review_Delete, name='review_delete'),
        path('views/',views.View_C_List.as_view(), name = 'view_C_detail'),
        path('view/<int:pk>/',views.View_C_info, name='view_C_info'),
        path('add_view_from_home',views.add_view_from_home, name = 'add_view_from_home'),


    ]

views.py

class View_C_Update_Form(forms.ModelForm):
        title = forms.TextInput( )#widget=forms.Textarea(attrs={'rows':2, 'cols':15,'value':'Title'}) )
        title.render('title','The title')
        view_text = forms.CharField( widget=forms.Textarea(attrs={'rows':10, 'cols':60  }))
        def __init__(self, *args, **kwargs):
            super(View_C_Update_Form, self).__init__(*args, **kwargs)
            #self.fields['title'].initial = 'This is default text.'
class View_C_List(ListView):
    model = View_C
 <form action="{% url 'uv:view_C_update' v.pk %}" id="update">{% csrf_token %}<input type="submit" id="update_button" value="update_{{v.pk}}" form="update"></input></form>

I should create a unique form id for each view (by attaching the pk to it) and then the buttons won't be all attached to the one form.

As you can see, I used form="update". This is done for every single button in the for loop. So obviously they now all point to the same form action, which is clearly undesirable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM