简体   繁体   中英

NoReverseMatch at /blah/blah/add though I thought my template if/else/endif would solve the error

I keep getting the error:

NoReverseMatch for 'grouplocation_add' with arguments '('',)' not found. 1 pattern(s) tried: [u'ipaswdb/grouplocations/add/(?P<pk>[0-9]+)/$']

After EDIT of adding context['group_id']=None on createview got this:

NoReverseMatch: Reverse for 'grouplocation_add' with arguments '(None,)' not found. 1 pattern(s) tried: [u'ipaswdb/grouplocations/add/(?P<pk>[0-9]+)/$']

The pattern:

url(r'^grouplocations/add/(?P<pk>[0-9]+)/$', GroupLocationCreateView.as_view(), name='grouplocation_add'),

Again if its an update where group_id exists, it works...if it is create new it is not but I thought my {% if ... %} and {% else %} would handle that scenario.

I am pretty sure it is my url 'ipaswdb:grouplocation_add group_id when group Id doesn't exist (it does if this is being called when an update is happening, but creating a new group, no id yet. The button this is on is to do more things for a group that has already been created. My If else was an attempt to say 'until the form has been submitted thusly the model saved thusly doing an update view with the model.... ' don't enable the button that lets you muck with the other parts of the model until it has been saved once.

  {% if group_id %}
    <td><a href=""><button disabled type="button" id="group_add_location" class="btn btn-primary">Add A Group 
    Location</button></a> </td>
  {% else %}
    <td><a href="{% url 'ipaswdb:grouplocation_add' group_id %}"><button type="button" id="group_add_location" class="btn btn-primary">Add A Group 
  {% endif %}

Given the error clearly my approach is different. I want to also save and return to the update view not the create view. So does that mean in my

 class GroupCreateView(CreateView):
         ..do tons of stuf
      def get_context_data(self, **kwargs): 
        ..do more stuf 
         context['group_id'] = None #added this
      def post(self, request, *args, **kwargs):
         if self.request.POST.has_key('submit'):
            ..DO LOTS OF STUFF..
         return HttpResponseRedirect("") 

Given once the page loads, that group_id exsists if it is loaded from the UpdateView then do I need the HttpResponseRedirect to go to the UpdateView somehow? How could I grab the model saved from the create view and pass it onto the updateview right in one step?

1) So I guess two things: Why is it giving that error when it is in an if else block.

2) once I fix that error how do I get the if else block to work by effectively turning around from the create view (the end that has the HttpResponseRedirect) and calling the UpdateView with the newly created model thusly having a group_id (that is passed into the contex in the UpdateView)

For the fact that in your else statement group_id is none. Therefore you should specify that field as none.

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