Started coding during lock down so I'm very new, what I've made may be a ugly monster so beware. Any help would be appreciated as google has abandoned me in my time of need.
I've added a form in a bootstrap modal which can be either Film/TV/Book. My issue is it always thinks its TV. This all worked when it had 3 different URL's and 3 separate POST requests but I wanted to feel fancy.
I'm trying to tell it with the 3 buttons what its "area_type" is using the button name and POST.
<div class="btn-group mr-2" role="group" aria-label="second group">
<div class="dropright">
<a class="btn btn-primary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Add Items
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" data-toggle="modal" data-target="#exampleModal" name="Film">Add Film</a>
</form>
<a class="dropdown-item" data-toggle="modal" data-target="#exampleModal" name="TV">Add TV</a>
<a class="dropdown-item" data-toggle="modal" data-target="#exampleModal" name="Book">Add Book</a>
</div>
</div>
</div>
<div class="form-group">
<form action="/{{franchises_details.franchise_slug}}/{{area|lower}}" method="POST" enctype="multipart/form-data">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{% csrf_token %}
{{franchises_details.franchies_name}}: {{area}}
{{form.name}}
{% if not area == "Film" %}
Number of Ep:{{form.number_of_episodes}}
{% endif %}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Film" name="Film"/>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
I set it to "TV" on the get request (only for the reason that it fell over when I left it blank) and I assume this is the first part of my issue. On a post request I check the for the button name.
def franchise_details(request,franchise_slug):
franchises_details = Franchise.objects.get(franchise_slug=franchise_slug)
form_type = forms.CreateFranchiseItemEp
if request.method == 'POST':
if "Film" in request.POST:
area = "Film"
form_type = forms.CreateFranchiseItemNoEp
if "TV" in request.POST:
area = "TV"
form_type = forms.CreateFranchiseItemEp
if "Book" in request.POST:
area = "Book"
form_type = forms.CreateFranchiseItemNoEp
form = form_type(request.POST)
if form.is_valid():
form_instance = form.save(commit=False)
form_instance.author = request.user
form_instance.franchies_name_id = franchises_details.id
form_instance.area_type = area
if area == "Film":
form_instance.number_of_episodes = 0
if area == "Book":
form_instance.number_of_episodes = 0
form_instance.all_user_average = 0
form_instance.save()
return redirect('/' + franchises_details.franchise_slug)
else:
form = form_type()
area = "TV"
return render(request, 'franchises_details.html', {'franchises_details': franchises_details, 'franchises_user': franchises_user, 'franchise_items_films':franchise_items_films, 'franchise_items_tvs':franchise_items_tvs,'franchise_item_users_film':franchise_item_users_film,'franchise_item_users_tv':franchise_item_users_tv, 'form':form,'area':area})
There's normally a bit more in here but i removed it for the question as its a tad busy and I think this is the only relevant part.
It looks to me that my "if "Film/TV/Book" in request.POST:" does nothing at all. Problem is this is what the intent is suggesting I do . Ether I've messed it up or its no longer a thing but I'm well and truly lost at this stage.
tl;dr Which button am I pressing? I push Film button it think its TV. It works as separate URL's with the same variables
As I said any help would be amazing as i'm mostly working off half knowledge and copy paste