This is in my template:
<form hx-post="{% url 'orders:create' %}">
{% csrf_token %}
{% for service in venue.services.all %}
<input type="checkbox" name="services" value="{{ service.id }}"> {{ service.name }}<br>
{% endfor %}
<button
hx-include="[name='id']"
type="submit"
class="btn btn-primary btn-lg ">
<input type="hidden" value="{{ venue.id }}" name="id">
Submit
</button>
</form>
And this is the view:
class OrderCreateView(CreateView):
model = Order
form_class = OrderForm
template_name = "orders/order_page.html"
success_url = reverse_lazy("orders:success")
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["venues"] = Venue.objects.all()
return context
def form_valid(self, form):
if self.request.htmx:
# Get the IDs of the chosen services
service_ids = self.request.POST.getlist('services')
# Set the venue field of the form
form.instance.venue = Venue.objects.get(id=self.request.POST.get("id"))
# Save the form
self.object = form.save()
# Add the chosen services to the Order object
for service_id in service_ids:
service = Service.objects.get(id=service_id)
self.object.chosen_services.add(service)
return super().form_valid(form)
The problem is that the object is being created but only the line with form.instance.venue works, the part where the chosen_services are being added doesn't work, the object is created without any of them.
The service_ids variable is populated with the information from the front end, it has the ids that i need, it just doesn't add them to the object.
This is models.py:
class Order(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.SET_NULL, null=True)
chosen_services = models.ManyToManyField(Service, null=True, blank=True)
Try this code
Here m2m field is already handled by form, you need to just set the value of the venue field with save() method of form
def form_valid(self, form):
if self.request.htmx:
# Get the IDs of the chosen services
service_ids = self.request.POST.getlist('services')
fm = form.save()
# Set the venue field of the form
fm.venue = Venue.objects.get(id=self.request.POST.get("id"))
fm.save()
return super().form_valid(form)
The problem was that in forms.py i had this:
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ["chosen_services"]
I deleted that and now it works!
Related
I am trying to use a modelchoicefield from the form in a formset_factory but i dont understand the error and don't know how to solve it.
(edit added the models.py)
models.py
class Order(Model):
user = ForeignKey(User, on_delete=SET_NULL, null=True)
fee = ForeignKey(Fee, on_delete=SET_NULL, null=True)
route = ForeignKey(Route, on_delete=SET_NULL, null=True)
price_rate = ForeignKey(PriceRate, on_delete=SET_NULL, null=True)
pallet_amount = IntegerField()
status = BooleanField()
postal_code = CharField(max_length=6)
city = CharField(max_length=255)
street = CharField(max_length=255)
delivery_from = DateTimeField()
delivery_until = DateTimeField(null=True)
created_at = DateTimeField(auto_now_add=True, blank=True)
updated_at = DateTimeField(auto_now=True)
deleted_at = DateTimeField(null=True, blank=True)
views.py
def routecreate_view(request):
orderformset = formset_factory(OrdersRouteForm, can_delete=False, extra=1)
if request.method == 'POST':
form = RouteForm(request.POST)
formset = orderformset(request.POST)
if form.is_valid() and formset.is_valid():
# process the data in form.cleaned_data as required
messages.success(request,
"You succesfully created an route.")
return HttpResponseRedirect(reverse('planner.dashboard'))
else:
form = RouteForm()
formset = orderformset()
return render(request, 'planner/route.html', {'form': form, 'formset': formset})
forms.py
class OrdersRouteForm(forms.ModelForm):
route = ModelChoiceField(
queryset=Order.objects.filter(status=1, delivery_until__gte=datetime.datetime.now(), deleted_at=None),
label='Order')
class Meta:
model = Order
fields = ("route",)
def __init__(self, *args, **kwargs):
super(OrdersRouteForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-control m-2'
self.fields['route'].label_from_instance = self.label_from_instance
#staticmethod
def label_from_instance(obj):
return "pallets: %s, %s, %s, %s" % (obj.pallet_amount, obj.street, obj.city, obj.postal_code)
template:
{% extends 'base.html' %}
{% block base %}
<div class="container rounded bg-white mt-5 mb-5">
<div class="row">
<div class="col-md-5 border-right mx-auto">
planner//route
<div class="p-3 py-5">
<form id="form-container" method="POST">
{% csrf_token %}
{{ form }}
{{ formset }}
<button id="add-form" type="button">Add Another Bird</button>
<button class="btn btn-danger profile-button mt-3" onclick="window.history.back()">Cancel
</button>
<button class="btn btn-primary float-end mt-3" type="submit">Order</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
error:
Cannot assign "<Order: Order object (2)>": "Order.route" must be a "Route" instance.
The error occurs when i fill the formset with request.POST and then try to send the formset to the template.
I have tried an inlineformset and modelformset and didnt get it to work. ANY solution is welcome.
The problem seemed to be with the modelform. I removed this from the code:
class Meta:
model = Order
fields = ("route",)
I don't understand why this caused the problem and if anyone knows and can explain it please feel free to do so.
hope it helps anyone else with this problem.
I recently started learning django and was making a CRM.
models.py:
class Complaint(models.Model):
SOURCE_CHOICES=(
('email','E-mail'),
('call','Call'),
('ticket','Ticket')
)
store_name=models.CharField(max_length=20)
store_contact_no=models.IntegerField(max_length=10)
store_id=models.CharField(max_length=7)
source=models.CharField(choices=SOURCE_CHOICES, max_length=10)
agent=models.ForeignKey("Agent", null = True, blank = True, on_delete=models.SET_NULL)
category=models.ForeignKey("Category", related_name="complaints", null = True, blank = True, on_delete=models.SET_NULL)
description = models.TextField()
customer = models.ForeignKey("Customer", null = True, blank = True, on_delete=models.SET_NULL)
def __str__(self):
return f"{self.store_name} {self.store_id}"
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
forms.py
class CustomerComplaintForm(forms.ModelForm):
class Meta:
model = Complaint
fields = (
'store_name',
'store_id',
'store_contact_no',
'description',
)
views.py
class
CustomerComplaintCreateView(OwnerCustomerAndLoginRequiredMixin,
generic.CreateView):
template_name = "customercomplaint_create.html"
form_class = CustomerComplaintForm
def get_success_url(self):
return "/complaints"
def form_valid(self, form):
complaint = form.save(commit=False)
complaint.Customer = self.request.user.username
complaint.source = 'ticket'
complaint.save()
return super(CustomerComplaintCreateView,
self).form_valid(form)
html template:
{% extends "base.html" %}
{% load tailwind_filters %}
{% block content %}
<div class="max-w-lg mx-auto">
<a class="hover:text-blue-500" href="/complaints">Go back to complaints </a>
<div class="py-5 border-t border-gray-200">
<h1 class="text-4xl text-gray-800"> Create a new complaint </h1>
</div>
<form method='post' class="mt-5">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="w-full bg-blue-500 text-white hover:bg-blue-600 px-
3 py-2 rounded-md">Create</button>
</form>
</div>
{% endblock content %}
mixins.py
class OwnerCustomerAndLoginRequiredMixin(AccessMixin):
"""Verify that the current user is authenticated and is an owner or customer"""
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated or not request.user.is_owner and not request.user.is_customer:
return redirect("/complaints")
return super().dispatch(request, *args, **kwargs)
The problem here is that, the source field gets filled in the database with Ticket as intended. But the 'Customer' field is not populated with the username. 'Self.request.user.username' is not the problem here as the username is being printed correctly in the console.
The issue is complaint.Customer = self.request.user.username, you're trying to assign a username to a supposedly Customer object. Here's an approach you could take to solve the issue though.
Within the views.py file, the view class.
You could get the customer object and then assign it to the customer field on the complaint object.
from django.shortcuts import get_object_or_404
def form_valid(self, form):
complaint = form.save(commit=False)
customer = get_object_or_404(Customer, user=self.request.user) # recommended
# or
# customer = get_object_or_404(Customer, user__username__iexact=self.request.user.username)
if customer:
# Here on your model you have a lowercase `c` for the customer field, not 'C`
complaint.customer = customer # -> This will assign the customer object, "FK".
complaint.source = 'ticket'
complaint.save()
return super(CustomerComplaintCreateView, self).form_valid(form)
That should work.
this must be the User not the user name
Because Cutomer is User object not only the Uesrname
def form_valid(self, form):
complaint = form.save(commit=False)
complaint.Customer = self.request.user
complaint.source = 'ticket'
complaint.save()
I'm going to receive data and save it using form and save it. But I can't get any result. Let me know what I'm doing wrong.
I set up a model. And I wrote a form to get the input. Forms.Form was used. At first, I used modelform, but I wrote it like this because there seemed to be no difference.
Is label important in the form? You can't get the data value because you can't connect the label?
heeelp!
models.py
class PayHistory(models.Model):
branch = models.ForeignKey(Branch, on_delete=models.CASCADE, null=True)
package_recommandation_date = models.DateField(null=True)
package_payment_date = models.DateField(null=True)
forms.py
class PackageForm(forms.Form):
package_recommandation_date = forms.CharField(label='package_recommandation_date')
package_payment_date = forms.CharField(label='package_payment_date')
...
## i changed like this(1)
class PackageForm(forms.ModelForm):
class Meta:
model = PayHistory
fields = ['package_recommandation_date', 'package_payment_date']
views.py
class PackageView(FormView):
model = PayHistory
template_name = 'branches/package-create.html'
success_url = reverse_lazy('/')
form_class = PackageForm
def form_valid(self, form):
form = form.save(commit=False)
form.save()
return super().form_valid(form)
### I realize what you mean. I changed it like this(2) and it was saved in DB.
def form_valid(self, form):
data = PayHistory()
data.package_recommandation_date = form.cleaned_data['package_recommandation_date']
data.package_payment_date = form.cleaned_data['package_payment_date']
data.save()
return super().form_valid(form)
# HTML
{% block content %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="table-content">
<!-- 검색 -->
<table border="0">
<tr class="input-tr">
<td><input type="date" class="input1" name="package_recommandation_date"
value="{{ form.package_recommandation_date.value|default_if_none:'' }}" required>
</td>
<td><input type="date" class="input2" name="package_payment_date"
value="{{ form.package_payment_date.value|default_if_none:'' }}">
</td>
...
<td><button type="submit" class="input-button input16">적용</button></td>
# static/js
const package_recommandation_date = document.querySelector("package_recommandation_date");
const package_payment_date = document.querySelector("package_payment_date");
console.info(package_recommandation_date, package_payment_date)
#output
-> null null
I was making a online store kind of website and am not able to make my add to cart option to work properly. I haven't yet linked the rest of the code to the button and am using an another link to operate it currently as you can see in the code.I want the form to submit the item name and brand automatically. Please suggest some way.
urls.py
url(r'^(?P<pk>[0-9]+)/addtocart/$', views.ItemAdd.as_view(), name='addtocart'),
models.py
class Mycart(models.Model):
name = models.CharField(max_length=250)
brand = models.CharField(max_length=250)
quantity = models.IntegerField(default='1')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('products:detail', kwargs={'pk': self.pk})
views.py
class ItemAdd(CreateView):
model = Mycart
fields = ['name', 'brand', 'quantity']
template_name = 'products/add_to_cart.html'
def get_context_data(self, **kwargs):
context = super(ItemAdd, self).get_context_data(**kwargs)
return context
add_to_cart.html
{% extends 'products/base.html' %} {% block body %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="hidden" name="name" value="{{ object.name }}">
<input type="hidden" name="brand" value="{{ object.brand }}">
<br>
<p>Enter Quantity</p>
<input type="number" name="quantity" value="">
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endblock %}
I understand that, when user click on item (product), you want automatically add name and brand to form, so user only need to enter quantity and submit form? Maybe you can try like this:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
product_pk = self.kwargs['pk']
product = Product.objects.get(pk=product_pk)
context.update({
'product': product
})
return context
Now you can access product in your template and get name and brand:
{{ product.name }}
{{ product.brand }}
You could use a Formview. Then you will have:
models.py
class Cart(models.Model):
quantity = models.PositiveIntegerField()
product = models.ForeignKey('products.Product')
forms.py
class AddCartForm(forms.ModelForm):
def save(self, product):
instance = super(AddCartForm, self).save(commit=False)
instance.product = product
instance.save()
return instance
class Meta:
model = Cart
fields = '__all__'
views.py
class AddCartView(FormView):
form_class = AddCartForm
success_url = '/'
def dispatch(self, request, *args, **kwargs):
product_pk = kwargs.get('product_pk')
self.product = get_object_or_404(Product, pk=product_pk)
return super(
AddCartView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kw):
context = super(AddCartView, self).get_context_data(**kw)
context.update(product=self.product)
return context
def form_valid(self, form):
form.save(product=self.product)
return super(AddCartView, self).form_valid(form)
add_cart.html
{% extends 'products/base.html' %} {% block body %}
<form action="{% url 'cart:add' product.pk %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endblock %}
I'm working with a CreateView where I know what some of the field values will be ahead of time. In the example below, I know that the author field for a new Entry object will be the current user and I use get_initial() to preset this.
Now I would like to omit this field from my template form. I've tried several approaches:
Simply commenting out the author field in the form template leads to an invalid form.
Leaving 'author' out of fields. Nope.
And here's a related problem. The example below involves a relationship to a User object that exists. But what if I need to create an object, say an auth Group for editors? I've tried creating a placeholder group and renaming it ... and, well, that didn't work very well.
#
# model
#
class Author(Model):
name = CharField(max_length = 60)
user = OneToOneField(to = User, related_name = 'author_user', on_delete = CASCADE)
class Entry(Model):
title = CharField(max_length = 60)
author = ForeignKey(to = Author, related_name = 'entry_author')
#
# view
#
class EntryCreateView(CreateView):
model = Entry
fields = ('title', 'author')
def get_initial(self):
initial = super(EntryCreateView, self).get_initial()
initial['author'] = get_object_or_404(Author, user = self.request.user)
return initial
#
# template
#
{% extends "base.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<label for="{{ form.title.id_for_label }}">Title:</label>
{{ form.title }}
<label for="{{ form.author.id_for_label }}">Author:</label>
{{ form.author }}
<p>
<input type="submit" class="btn btn-primary" name="save" value="Save" />
<input type="submit" class="btn btn-primary" name="cancel" value="Cancel" />
</form>
{% endblock %}
You can manually set user in form_valid() method of EntryCreateView class:
class EntryCreateView(CreateView):
model = Entry
fields = ('title',)
def form_valid(self, form):
user = self.request.user
form.instance.user = user
return super(EntryCreateView, self).form_valid(form)
You'll need to create a ModelForm for the customizations you need (https://docs.djangoproject.com/en/1.9/topics/forms/modelforms/).
You can't remove author because it's required on your model currently.
Try something like this:
In forms.py...
class EntryForm(forms.ModelForm):
class Meta:
model = Entry
fields = ['title', 'author']
def __init__(self, *args, **kwargs):
initial = kwargs.get('initial', {})
self.author = initial.get('author')
super(EntryForm, self).__init__(*args, **kwargs)
You can make modifications to the fields (set to not required, delete a field from the form fields, etc) in __init__ or on the class.
Just import and reference this form in your views to use it.