简体   繁体   English

Django按钮重定向错误

[英]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. 我希望用户能够单击该视图的“ update_button”(称为v)并定向到该页面。 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. 如果我将表单操作中的v.pk替换为9,那么它将正确地将我重定向到主键9。但是,如果我再次用v.pk替换硬编码的9,然后单击按钮(对于pk = 9的帖子),它将重定向我到pk = 7(该用户实际上可以更新的帖子的最低pk),但这是错误的pk,当我将print(pk)放在查看它打印7,而不是9,因此链接中放置的值实际上不是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. 就像按钮已损坏,同时与inspect元素正确显示一样。

{% 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 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 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. 我应该为每个视图创建一个唯一的表单ID(将pk附加到该视图),然后按钮将不会全部附加到一个表单。

As you can see, I used form="update". 如您所见,我使用了form =“ update”。 This is done for every single button in the for loop. 对于for循环中的每个单个按钮都可以完成此操作。 So obviously they now all point to the same form action, which is clearly undesirable. 因此很明显,他们现在都指向同一个表单动作,这显然是不希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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