views.py
def brand(request):
form = BrandForm()
if request.method == 'POST':
form = BrandForm(request.POST)
if form.is_valid():
brand = form.save(commit=False)
brand.brandname = form.cleaned_data['brandname']
brand.save()
transaction.commit()
return redirect('brand_list')
else:
print(form.errors)
return render(request, 'brand.html', {'form': form})
models.py
class Brands(models.Model):
brandname = models.CharField(max_length=25, unique=True)
forms.py
class BrandForm(forms.ModelForm):
class Meta:
model = Brands #the model the form is associated with
fields = ['brandname']
brand.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylsheet" href="product.css">
</head>
<body>
<div id=>
<form method="POST " action="{% url 'brand' %}">
{% csrf_token %}
{{ form.as_p }}
<button type="submit"> Submit </button>
</form>
</div>
</body>
</html>
In urls.py I have used brand for the pathname in urlpatterns
I am not able to insert data into the brands table in Mysql