简体   繁体   中英

Python/Django - How to pass value from html to view via {% url %}?

I have a Django application that contains a form where the user can select " choice1 " or " choice2 ". So what I'm trying to do is that when an user gives an input via the radiobutton in the html and clicks "Update", it sends the new values ( view.choice1 and view.valuechoice2 which are Boolean values) of to my a view, and prints the new values in my console.

What I have is the following:

choiceMenu.html

<form class="card" action="" method="POST" enctype="multipart/form-data">
    <input type="radio" name="update_choice" value="choice1">  Choice1 </input>
    <input type="radio" name="update_choice" value="choice2">  Choice2 </input>
    <div class="form-group">
        <a href="{% url 'core:choicemenu:choices' view.choice1 %}" class="btn btn-secondary btn-sm">Update</a>
    </div>
</form>

model.py

class ChoiceModel(models.Model):
    choice1 = models.BooleanField(default=False)
    choice2 = models.BooleanField(default=True)

    def get(self):
        new_self = self.__class__.objects.get(auto=self.choice1)

        self.__dict__.update(new_self.__dict__)
        return reverse("core:choices", kwargs={"choice1": self.choice1})

views.py

class ChoiceMenu(generic.TemplateView):
    template_name = 'core/choiceMenu.html'
    context_object_name = 'choicemenu'
    current_model_set = ChoiceModel.objects.get(id=1)

    choice1 = int(current_model_set.choice1 == True)
    choice2 = int(current_model_set.choice2 == True)


class ChoiceSetting(generic.TemplateView):
    extra_context = {"choices_page": "active"}
    context_object_name = 'choices'
    template_name = 'core/choices/choiceindex.html'

    def get(self, queryset=None, **kwargs):
        choice1 = self.kwargs.get("choice1")

        logger.info(choice1)  ### <<-- I want to get this printed in my console

        return redirect(reverse("core:choicemenu"))

urls.py

app_name = 'core'
    urlpatterns = [
        path('choicemenu', login_required(views.ChoiceMenu.as_view()), name='choicemenu'),
        path('choicemenu/choices/<int:choice1>/', login_required(views.ChoiceSetting.as_view()), name='choices')
]

So, what I want is that when the user selects choice1 and pushes the button Update , it prints 1 in my console.

The error I get with this code is:

django.urls.exceptions.NoReverseMatch: Reverse for 'choices' with arguments '('',)' not found. 1 pattern(s) tried: ['choicemenu/choices/\\?P(?P<choice1>[^/]+)\\\\w\\+\\)\\$/\\Z']

Does somebody know how I can get this fixed..?

Use the post method in view like

class ChoiceSetting(generic.TemplateView):
extra_context = {"choices_page": "active"}
context_object_name = 'choices'
template_name = 'core/choices/choiceindex.html'

def post(self, *args, **kwargs):
    choice1 = self.request.POST.get('update_choice')

    logger.info(choice1)  ### <<-- I want to get this printed in my console

    return redirect(reverse("core:choicemenu"))

and in urls.py use

path('choicemenu/choices/', login_required(views.ChoiceSetting.as_view()), name='choices')

and in html file

<form class="card" action="{% url 'core:choicemenu:choices' %}" method="POST" enctype="multipart/form-data">{% csrf_token %}<input type="radio" name="update_choice" value="choice1">  Choice1 </input>
<input type="radio" name="update_choice" value="choice2">  Choice2 </input>
<div class="form-group">
    <button class="btn btn-secondary btn-sm" type="submit">Update</a>
</div>

you can get your expected output

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