In Django I have two functions contained in views.py
. One is called def city_detail
and the other is called def DemoView
. Both return return render(request, 'app1/home.html', context)
.
In urlpatterns
of url.py
i would like to use both functions, but it seems I can only use 1 at a time. If I use only one of the two, then it works (only one of the two works). If use both functions i get error:
path('home/', views.city_detail, views.DemoView, name='home'),
path('home/', views.DemoView, name='home'),
I get the error:
raiseTypeError(
TypeError: kwargs argument must be a dict, but got function.
How can I use both functions in home?
UPDATE
I update the question by writing the whole code
I believed there was no need for the code as I believed the solution was simpler. Update to improve the question. I added the code that I use in the project.
home.html
is the page I go to after logging in
app1/urls.py
from django.urls import path, include
from . import views
from app1.views import index
urlpatterns = [
path('', index, name='index'),
path('home/', views.city_detail, views.DemoView, name='home'),
path('home/', views.DemoView, name='home'),
]
app1/views.py
from django.shortcuts import render, redirect
#Simple Combobox
def DemoView(request):
color_choices = ("Red","Blue","Black","Orange")
message =''
if request.method =="POST":
picked = request.POST.get('color')
if picked == 'Red':
message = "<<< You chose red"
print(message)
elif picked == 'Blue':
message = "<<< You chose blue"
elif picked == 'Black':
message = "<<< You chose black"
else:
message = "<<< Oh no, you chose orange"
context = {'message':message,'color_choices':color_choices}
return render(request, 'app1/home.html', context)
#Combobox from Database
def city_detail(request):
form = SelectCityForm()
object = None
if request.method == "POST":
form = SelectCityForm(request.POST)
if form.is_valid():
city_id = form.cleaned_data['city']
object = City.objects.get(id=city_id)
context = {'form': form, 'object': object}
return render(request, 'app1/home.html', context)
app1/models.py (for combobox from database)
from django.db import models
class City(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
# here you want to return the name
# print function on an instance of this model then returns the name
return self.name
app1/forms.py
from django import forms
#Combobox Semplice
class SimpleCombobox(forms.Form):
Col1 = 'Red'
Col2 = 'Blue'
Col3 = 'Black'
Col4 = 'Orange'
COLOR_CHOICES = (
(Col1, u"Red"),
(Col2, u"Blue"),
(Col3, u"Black"),
(Col4, u"Orange"),
)
cities = forms.ChoiceField(choices=COLOR_CHOICES)
class SimpleTextbox(forms.Form):
coverletter = forms.CharField(required=False,
widget=forms.Textarea(
# rows and colums of the textarea
attrs={'rows': 4, 'cols': 40}))
from .models import City
class SelectCityForm(forms.Form):
city = forms.ModelChoiceField(queryset=City.objects.all())
class Meta:
model = City
fields = ('name')
app1/home.html
<div class="b-example-divider b-example-vr"></div>
<div class="app">
<form action="" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% if object %}
Here you can embed data of your city such as the ID: {{ object.id }} or the Name: {{ object.name }}. Since you defined the __str__ method you can also
just put {{ object }} and it will give the name.
{% endif %}
</div>
<div class="container p-5">
<div class="row mx-auto">
<div class="col-6">
<form action="" method="POST" novalidate class="form-group">
{% csrf_token %}
<select name="color" class="form-select" >
{% for i in color_choices %}
<option value="{{i}}">{{i}}</option>
{% endfor %}
</select>
<textarea class="form-control" name="msg" cols="30" rows="10">{{message}}</textarea>
<button class="btn btn-primary mt-3" type="submit">Submit</button>
</form>
</div>
</div>
</div>