Python/Django - 如何将值从 html 传递到通过 {% url %} 查看?
Python/Django - How to pass value from html to view via {% url %}?
我有一个 Django 应用程序,其中包含一个表单,用户可以在其中 select "choice1
" 或 "choice2
" 。所以我想做的是,当用户通过 html 中的单选按钮输入并单击“更新”时,它会发送新值(view.choice1
和 view.valuechoice2
是布尔值)到我的视图,并在我的控制台中打印新值。
我有以下内容:
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')
]
所以,我想要的是当用户 selects choice1
并按下按钮 Update
时,它会在我的控制台中打印 1
。
我用这段代码得到的错误是:
django.urls.exceptions.NoReverseMatch: Reverse for 'choices' with arguments '('',)' not found. 1 pattern(s) tried: ['choicemenu/choices/\?P(?P<choice1>[^/]+)\\w\+\)\$/\Z']
有人知道我该如何解决这个问题吗?
在视图中使用post方法
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"))
并在 urls.py 中使用
path('choicemenu/choices/', login_required(views.ChoiceSetting.as_view()), name='choices')
并在 html 文件中
<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>
你可以得到你预期的输出
我有一个 Django 应用程序,其中包含一个表单,用户可以在其中 select "choice1
" 或 "choice2
" 。所以我想做的是,当用户通过 html 中的单选按钮输入并单击“更新”时,它会发送新值(view.choice1
和 view.valuechoice2
是布尔值)到我的视图,并在我的控制台中打印新值。
我有以下内容:
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')
]
所以,我想要的是当用户 selects choice1
并按下按钮 Update
时,它会在我的控制台中打印 1
。
我用这段代码得到的错误是:
django.urls.exceptions.NoReverseMatch: Reverse for 'choices' with arguments '('',)' not found. 1 pattern(s) tried: ['choicemenu/choices/\?P(?P<choice1>[^/]+)\\w\+\)\$/\Z']
有人知道我该如何解决这个问题吗?
在视图中使用post方法
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"))
并在 urls.py 中使用
path('choicemenu/choices/', login_required(views.ChoiceSetting.as_view()), name='choices')
并在 html 文件中
<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>
你可以得到你预期的输出