1

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">&times;</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

Paul
  • 57
  • 7

2 Answers2

1

Ok turns out i'm an idiot. I was running this code at the old URL the whole time which was set up for TV

Also the button name is Film so I needed a button for each submit but it works now

Paul
  • 57
  • 7
  • :) that how you learn sometimes. Still I reccomend you to build an API in the future... ;-) Good luck with everything ! :-) – mama Jul 07 '20 at 21:26
  • @mama haha thanks, I'll have to have a look into it. I have a friend whos been setting up REST for somthing ebay related so I might badger him depending how far I get with tutorials – Paul Jul 12 '20 at 22:19
0

I got similar problems when I started using Django. I was building a reservation form for a restaurant and I wanted the form to change on the fly, depending on the inputs from the user.

The problem is that you want to get the backend to control the frontend logic, and that is very inefficient.

There is 2 things you can do.

  1. Accept that Django's build in views are limited and you can not do fancy frontend stuff, so the user experience is gonna suck.
  2. Or you could start learning how to set up Django-REST-Framework, which is gonna be a whole new world. Then you will keep Django as a backend (ONLY) server, and then you can use JavaScript for frontend logic, with eg. JQuery which is the easiest to learn.

note: If you pick option 2, then in the future you can focus on learning GraphQL which is a more modern version of REST (but less documented), and REACTjs instead of JQuery. And I say in the future because REST and JQuery is easier to learn than the other 2's

Good luck with the programming, I hope this will help :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mama
  • 2,046
  • 1
  • 7
  • 24
  • So I might be wrong but I dont think this is an issue with views, I'm not trying to change the visibility of anything, just what i fill in the from with area_type with TV/Film/Book. I think its more my code failing to identify the button name from the POST like other answers seem to suggest. https://stackoverflow.com/questions/2159795/django-request-post-does-not-contain-the-name-of-the-button-that-submitted-the-f Again, very new so might be talking out rear end – Paul Jul 07 '20 at 20:07