Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - NoReverseMatch at / UpdateView

I try to use an updateview with a form. I get the error:

 Reverse for 'response' with arguments '('',)' not found. 1 pattern(s)
tried: ['app/response/(?P<pk>[-a-zA-Z0-9_]+)/$']

views.py:

class ResponseUpdateView(UpdateView):
    model = Model
    fields = ['response']

urls.py

path( route='response/<slug:pk>/', view=views.ResponseUpdateView.as_view(), name='response' )

template:

   {% extends "base.html" %}     
   {% load crispy_forms_tags %}     
   {% block title %}{% endblock %}     
   {% block content %}
    
   <form method="post" action="{% url 'tower:response' slug %}">
        {% csrf_token %}
        {{ form|crispy }}
        <button type="submit" class="btn btn-primary">Send</button>   </form>
    
    
    {% endblock %}

the error is in the template {% url 'app:response' pk %} specifically pk, I have tried using:

def get_context_data(self, **kwargs):
        context = super(ResponseUpdateView, self).get_context_data(**kwargs)
        context['slug'] = self.kwargs['pk']

together with {% url 'app:response' slug %}

If I don't add the url everything works fine

question from:https://stackoverflow.com/questions/65882205/noreversematch-at-updateview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You simply forgot to return the context. The following should work:

def get_context_data(self, **kwargs):
        context = super(ResponseUpdateView, self).get_context_data(**kwargs)
        context['slug'] = self.kwargs['pk']
        return context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...