Form select options values are there but cannto be seen

form

<div class="container mt-2">
    <form method="GET">
        {% csrf_token %}
        <h1><center>Transfer a Fish</center></h1><br>
        <select class="form-select">
            <option value="null">Select a Fisher</option>
            {% for sponsor in sponsors  %}
            <option value="{{ sponsor.id }}">{{ sponsor.alias_name }}</option>
            {% endfor %}
          </select>
    
        <br><br><div>{{ form.as_p }}
        <input type="submit" value="Submit">
        </div> 
    </form>
    </div>

The select has two options, I have tested it to have one and it shows one so the data is there, that can be selected but there is not text it is just blank.

Please post the view that is rendering this template.

Data is correct.

def fish_transfer(request):
    form = TransferFishForm

    sponsors = CustomUser.objects.filter(sponsor=request.user.id).filter(Q(status__range=(3,6))).values_list('id', 'alias_name')

    context = {
        'sponsors': sponsors,
    }
    return render(request, 'accounts/fish_transfer_form.html', context)

Because you are using:

the sponsors object is a queryset containing a list. This list does not associate attribute names with the values.

Remove the values_list function call from the query.

Side note: There are very few cases where you want to use that when rendering objects from models.

Side note 2: You don’t need to use two separate filter calls and you don’t need to wrap that second filter inside a Q object.

Also is it possible to do this in a modal that is an item in a dropdown?

Do what? I’m not understanding what you’re asking about.

Right now I am using a template and was just curious if it could be a modal. Anyway, I am unable to get the value to show when I send it to a new view.

I select it in the select and submit and the queryset printed from the view sent to is empty.

<div class="container mt-2">
    <form >
        {% csrf_token %}
        <h1><center>Transfer a Fish</center></h1><br>
        <h3><center> {{ user.alias_name}}, you are requesting a transfer of {{ fish }} to: </center></h3><br>
        <div><center>
            <select class="form-select w-25" id="'new_sponsor">
                <option value="null">Select a Fisher</option>
                {% for new_sponsor in sponsors  %}
                <option value="{{ new_sponsor.id }}">{{ new_sponsor.alias_name }}</option>
                {% endfor %}
            </select></center>
        </div><br><br>
        <center><a href="{% url 'fish_transfer_send' fish  %}" class="btn btn-success">Transfer</button></a>
        <a href="{% url 'accounts/fish_list' %}" class="btn btn-danger">Cancel</button></a></center>
        </div> 
    </form>
    </div>
@login_required
def fish_transfer_send(request, fish):

    print(request.user.alias_name)
    print(fish)
path('fish_transfer_send/<str:fish>/', views.fish_transfer_send, name='fish_transfer_send'),

print output = jeff_l0000, test_u4789

Your button is a button as a link - it is not a submit button and so it’s not submitting the form.

Regarding your question about modals, that’s a templating and JavaScript issue it has nothing to do with the view itself.

form

class FishTransferForm(forms.Form):

    new_sponsor = forms.CharField(max_length=25)

    class Meta:
        fields = ('new_sponsor')

views

def fish_transfer(request, alias_name):
    sponsors = CustomUser.objects.filter(sponsor=request.user.id, status__range=(3,6)).values('id', 'alias_name')

    context = {
        'sponsors': sponsors,
        'fish': alias_name
    }
    return render(request, 'accounts/fish_transfer.html', context)

@login_required
def fish_transfer_send(self, form):
        form = FishTransferForm
        print(self)
        print(form.data)

URLs

path('fish_transfer/<str:alias_name>/', views.fish_transfer, name='fish_transfer'),

template

{% block body %}
    <div class="container mt-2">
    <form method="POST">
        {% csrf_token %}
        <h1><center>Transfer a Fish</center></h1><br>
        <h3><center> {{ user.alias_name}}, you are requesting a transfer of {{ fish }} to: </center></h3><br>
        <div><center>
            <select class="form-select w-25">
                <option value="null">Select a Fisher</option>
                {% for new_sponsor in sponsors  %}
                <option value="{{ new_sponsor.id }}">{{ new_sponsor.alias_name }}</option>
                {% endfor %}
            </select></center>
        </div><br><br>
        <center><button type="submit">Transfer</button>
        <a href="{% url 'accounts/fish_list' %}" class="btn btn-danger">Cancel</button></a></center>
        </div> 
        {{ form.as_p }}
    </form>
    </div>
    {% endblock body %}

It is not making it to the fish_transfer_send view.

[30/Oct/2023 15:38:37] “POST /accounts/fish_transfer/test_u4789/ HTTP/1.1” 200 5510

You’re defining a form, but not using it at all - you could remove it. (Or, I would actually recommend using it.)

You don’t have a name on your select widget for the value to be submitted. (You’re rendering that widget manually - I’d suggest rendering it appropriately as a form.)

Your view doesn’t handle the posting of the form.

I suggest you review the docs at Working with forms | Django documentation | Django to gain an understanding of how Django works with forms and how you should interact with them.

I think I am close to getting this. I have the form being rendered as a select but not getting the values. The two ways that I have seen it done is through context or importing the view in the forms. With the context, nothing shows, and with the import, I get a circular import error.

list view

def fishers_list(request):
    fishers = CustomUser.objects.filter(sponsor=request.user.id, status__range=(3,6)).values('alias_name').distinct('alias_name')

    fishers_list(request, fishers)

view with context

@login_required
def fish_transfer(request, alias_name):
    
    fishers = CustomUser.objects.filter(sponsor=request.user.id, status__range=(3,6)).values('alias_name').distinct('alias_name')
    
    if request.method == "POST":
        form = FishTransferForm(request.POST)
        print(request.POST)
        if form.is_valid():
            print(form.cleaned_data)
        return HttpResponse('<h1>Transfer Submitted</h1>')
    
    else:
        form = FishTransferForm()
    return render(request, 'accounts/fish_transfer.html', context = {'form':form, 'fishers':fishers, 'fish':alias_name})

form

class FishTransferForm(forms.Form):

    fishers = forms.ChoiceField(label='Select a Fisher', choices=[], widget=Select)
    
    class Meta:
        fields = ('fishers',)

template

 {% block body %}
    <div class="container mt-2">
    {% csrf_token %}
    <form  method="POST" >
        {% csrf_token %}
        <h1><center>Transfer a Fish</center></h1><br>
        <h3><center> {{ user.alias_name}}, you are requesting a transfer of {{ fish }} to: </center></h3><br>
        <center>{{ form }}</center>
        </div><br><br>
        <center><button type="submit" class="btn btn-success">Transfer</button>
        <a href="{% url 'accounts/fish_list' %}" class="btn btn-danger">Cancel</button></a></center>
        </div> 
        
    </form>
    </div>
    
    {% endblock body %}

Also, this is a way I have found form = FishTransferForm() by passing it in here. Can’t get it to work either.

The fishers should be part of the form, it shouldn’t be something defined separately.

Your form needs to be defined to reference the options.

In practice, you would do this by using a ModelChoiceField instead of a ChoiceField.

I suggest you find some blogs, books, or other resources that show a complete example of creating and using a form. (See GitHub - wsvincent/awesome-django: A curated list of awesome things related to Django for resources.)

Unfortunately, the official docs don’t provide a complete example that puts all the pieces together, so that’s actually not the best resource to see how it’s done in practice. There probably are a few examples here in the forum as well if you want to do some searching.

Thanks and I have been searching and have not them yet.