I am very new to Django and can use some help. I’ve created a manual form in a html file, (my custnames,html file), and it works fine. It contains a database query “select” section and an “onchange submit form” entry. For the life of me I cannot move it to my forms.py file and still have it work. The functionality is for the user to select a customer from a drop-down list and as soon as that is done, a GET is sent. This works because I can see the “nameocust=nn” entry in the URL. I would appreciate any suggestions that anyone can forward to me.
~Ed
models.py
from django.db import models
This will hold the team members
class CustTeamMembers(models.Model):
member_first_name = models.CharField(max_length = 100)
member_last_name = models.CharField(max_length = 100)
member_email = models.EmailField(unique=True)
member_phone = models.CharField(max_length = 25, blank=True)
class Meta(object):
ordering = ['member_last_name']
def __str__(self):
return self.member_last_name
This will hold the customer name
class CustTeamName(models.Model):
cust_name = models.CharField(max_length = 100)
cust_members = models.ManyToManyField(CustTeamMembers, blank=True)
cust_meet = models.CharField(max_length = 40, blank=True)
cust_status = models.TextField(blank=True)
def __str__(self):
return self.cust_name
def get_members(self):
return ", ".join([str(p) for p in self.cust_members.all()])
custnames.html
{% extends ‘custdat/base.html’ %}
{% block ‘body’ %}
<nav class="navbar navbar-expand-lg navbar-dark" style="background-color: #238a44">
<div class="container">
<a class="navbar-brand" href="{% url 'home' %}">The Home Button</a>
</div>
</nav>
<center><h1>CUSTOMER TEAM PAGE</h1></center>
<div>
<form action="{% url 'custnames' %}" method="get">
{% csrf_token %}
<center>
<h2>Choose A TISS Customer Team To Review</h2>
<select id="nameocust" name="nameocust" onchange="this.form.submit();">
<option value="0" selected disabled>Select Team</option>
{% for custname in custnamesall %}
<option value="{{ custname.id }}">{{ custname.cust_name }}</option>
{% endfor %}
</select>
</center>
</form>
</div>
{% endblock %}
views.py
from django.shortcuts import render
#from .forms import CustForm
from .models import CustTeamMembers, CustTeamName
Create your views here.
def home(request):
return render(request, ‘custdat/home.html’)
def custnames(request):
custnamesall = CustTeamName.objects.all()
return render(request, ‘custdat/custnames.html’, { ‘custnamesall’: custnamesall,})
forms.py (As it is now)
from django import forms
class CustForm(forms.Form):
cust_name = forms.CharField(label=‘Customer Name’, max_length=100)
cust_status = forms.CharField(label=‘Customer Status’)