didn't return an HttpResponse - django

Good morning, when I try to send the form, I get the error, and when I send it, it generates that my view does not return any httpresponse object.
this is the view
class ProductView(View):
template_name = 'products/product.html'
model = Product
form_class = ProductForm
def get_queryset(self):
return self.model.objects.filter(state=True)
def get_context_data(self, **kwargs):
context = {}
context['product'] = self.get_queryset()
context['list_product'] = self.form_class
return context
def get(self, request, *args, **kwargs):
return render(request, self.template_name, self.get_context_data())
def post(self, request, *args, **kwargs):
list_product = self.form_class(request.POST)
if list_product.is_valid():
list_product.save()
return redirect('products:product')
and this is the form
class ProductForm(forms.ModelForm):
name_product = forms.CharField(
max_length=25,
widget=forms.TextInput(
attrs={
'class': 'form-control',
'id': 'name_product',
}
)
)
def clean_name_product(self):
name_product = self.cleaned_data.get('name_product')
if Product.objects.filter(name_product=name_product).exists():
raise forms.ValidationError('El nombre del producto ya existe')
return name_product
class Meta:
model = Product
fields = (
'name_product', 'description', 'price', 'category', 'state', 'image'
)
labels = {
'name_product': 'Nombre del Producto',
'description': 'Descripcion',
'price': 'Precio',
'category': 'Categoria',
'state': 'Estado',
'image': 'Imagen del Producto',
}
widgets = {
'name_product': forms.TextInput(
attrs={
'class': 'form-control',
'id': 'name_product',
}
),
'description': forms.TextInput(
attrs={
'class': 'form-control',
'id': 'description',
}
),
'price': forms.NumberInput(
attrs={
'class': 'form-control',
'id': 'price',
}
),
'category': forms.SelectMultiple(
attrs={
'class': 'custom-select',
'id': 'category',
}
),
'state': forms.CheckboxInput(),
}
when I give send it generates the error.The view products.views.ProductView didn't return an HttpResponse object. It returned None instead.
At the beginning I thought that the error is due to the error lifting in the form, change the code without the validation and it generates the same error

In case the form.is_valid() fails, you do not return anything in the post method, hence the error. That being said, this is basically just a CreateView [Django-doc], so it might be better to use that to reduce the amount of "boilerplate" code:
from django.views.generic.edit import CreateView
class ProductView(CreateView):
template_name = 'products/product.html'
model = Product
form_class = ProductForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(
product=Product.objects.filter(state=True),
list_product=context['form']
)
return context

Related

How to serialize the foreign key field in django rest framework

I work on a project with DRF where I'm getting serializer data as follows which is absolutely fine:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": 2,
"item": 1,
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
but I want to get as follows:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": "user name",
"item": "product name",
"price: "3.44",
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
I tried using DRF RelatedField and PrimaryKryRelatedField but in all these cases I need to make corresponding fields as read_only=True which I want to skip.
I also tried with depth = 1 which gives entire details
My Model:
class Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
item = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return "{} - {} - {} - {} - {}".format(self.user,
self.item,
self.quantity,
self.created_at,
self.updated_at)
My serializer:
class CartSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
My View:
class CartViewSet(viewsets.ModelViewSet):
queryset = Cart.objects.all().order_by('id')
serializer_class = CartSerializer
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.serializer_class(
queryset, context={'request': request}, many=True)
data = serializer.data
context = {
'message': 'Listed Successfully',
'count': queryset.count(),
'errors': False,
'data': data,
}
return Response(context, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs):
if not request.data:
return Response("No data", status=status.HTTP_400_BAD_REQUEST)
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
self.perform_create(serializer)
context = {
'message': 'Created Successfully',
'status': status.HTTP_201_CREATED,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(
instance, data=request.data, partial=partial)
if serializer.is_valid():
self.perform_update(serializer)
context = {
'message': 'Updated Succesfully',
'status': status.HTTP_200_OK,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
context = {
'message': 'Deleted Succesfully',
'status': status.HTTP_204_NO_CONTENT,
'errors': False,
}
return Response(context)
#action(methods=['get'], detail=False, url_path='checkout/(?P<userId>[^/.]+)', url_name='checkout')
def checkout(self, request, *args, **kwargs):
try:
user = User.objects.get(pk=int(kwargs.get('userId')))
except Exception as e:
return Response(status=status.HTTP_404_NOT_FOUND, data={'Error': str(e)})
cart_helper = CartHelper(user)
checkout_details = cart_helper.prepare_cart_for_checkout()
if not checkout_details:
return Response(status=status.HTTP_404_NOT_FOUND,
data={'error': 'Cart of user is empty.'})
return Response(status=status.HTTP_200_OK, data={'checkout_details': checkout_details})
Please if anyone can help, it would be greatly appreciated.
Thanks
You can make use of to_representation()
Give this a try
class CartSerializer(serializers.ModelSerializer):
class Meta:
model = Cart
def to_representation(self, instance):
representation = dict()
representation["id"] = instance.id
representation["user"] = instance.user.username
representation["item"] = instance.item.name
representation["quantity"] = instance.quantity
representation["created_at"] = instance.created_at
representation["updated_at"] = instance.updated_at
return representation
NB: You may have to change instance.field_name(s) accordingly

How to Foreign_Key value instead of id in django rest framework without read_only=True

I working on project with drf where I'm getting serializer data as follows which is absolutely fine:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": 2,
"item": 1,
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
but I want to get as following:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": "user name",
"item": "product name",
"price: "3.44",
"quantity": 4,
"created_at": "2021-08-11T13:49:27.391939Z",
"updated_at": "2021-08-11T13:51:07.229794Z"
}
}
I tried using drf RelatedField and PrimaryKryRelatedField but in all these cases I need to make corresponding fields as read_only=True which I want to skip.
I also tried with depth = 1 which gives entire details
My Model:
class Cart(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
item = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
quantity = models.IntegerField(null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return "{} - {} - {} - {} - {}".format(self.user,
self.item,
self.quantity,
self.created_at,
self.updated_at)
My serializer:
class CartSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
My View:
class CartViewSet(viewsets.ModelViewSet):
queryset = Cart.objects.all().order_by('id')
serializer_class = CartSerializer
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.serializer_class(
queryset, context={'request': request}, many=True)
data = serializer.data
context = {
'message': 'Listed Successfully',
'count': queryset.count(),
'errors': False,
'data': data,
}
return Response(context, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs):
if not request.data:
return Response("No data", status=status.HTTP_400_BAD_REQUEST)
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
self.perform_create(serializer)
context = {
'message': 'Created Successfully',
'status': status.HTTP_201_CREATED,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(
instance, data=request.data, partial=partial)
if serializer.is_valid():
self.perform_update(serializer)
context = {
'message': 'Updated Succesfully',
'status': status.HTTP_200_OK,
'errors': serializer.errors,
'data': serializer.data,
}
return Response(context)
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
context = {
'message': 'Deleted Succesfully',
'status': status.HTTP_204_NO_CONTENT,
'errors': False,
}
return Response(context)
#action(methods=['get'], detail=False, url_path='checkout/(?P<userId>[^/.]+)', url_name='checkout')
def checkout(self, request, *args, **kwargs):
try:
user = User.objects.get(pk=int(kwargs.get('userId')))
except Exception as e:
return Response(status=status.HTTP_404_NOT_FOUND, data={'Error': str(e)})
cart_helper = CartHelper(user)
checkout_details = cart_helper.prepare_cart_for_checkout()
if not checkout_details:
return Response(status=status.HTTP_404_NOT_FOUND,
data={'error': 'Cart of user is empty.'})
return Response(status=status.HTTP_200_OK, data={'checkout_details': checkout_details})
Please if anyone can help, it would be greatly appreciated.
Thanks
Update your CartSerializer to_response method to dynamically change/add the value outputs for the specific fields. Something like this with the appropriate name should do the trick
class CartSerializer(serializer.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
class Meta:
model = Cart
fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
def to_representation(self, instance):
response = super().to_representation(instance)
response['user'] = instance.user.name
response['item'] = instance.item.name
response['price'] = instance.item.price
return response

Create view with more models

hi everyone I have a doubt with the use of forms and models.
I have to create a code that creates records in multiple tables and I don't know how to do it.
my goal is to create a page where I can enter all the data and when I save it creates the various tables filled in with the data provided by the user.
I'm a beginner I still have to learn the mechanism well =)
forms.py
from django import forms
from .models import Schede, DatiGruppi, Gruppi
class CreaSchedaForm(forms.ModelForm):
nome_scheda = forms.CharField(
required = True,
label ='Nome scheda',
widget = forms.TextInput(
attrs = {
'class': 'form-control',
'placeholder' : 'nome scheda',
'autocomplete' : 'off'
}
)
)
data_inizio = forms.DateField(
label='Data inizio',
widget = forms.DateInput(
attrs= {
'type': 'date',
'class': 'form-control',
'placeholder' : 'data inizio'
}
)
)
data_fine = forms.DateField(
label='Data fine',
widget = forms.DateInput(
attrs= {
'type': 'date',
'class': 'form-control',
'placeholder' : 'data fine'
}
)
)
class Meta:
model = Schede
fields = ['nome_scheda','data_inizio','data_fine']
class CreaDtGruppoForm(forms.ModelForm):
giorni_settimana = forms.ChoiceField(
choices = DatiGruppi.giorni_settimana_scelta
)
dati_gruppo = forms.ModelChoiceField(
queryset = Gruppi.objects.all(),
empty_label = "-",
required = True
)
class Meta:
model = DatiGruppi
fields = ['giorni_settimana', 'dati_gruppo']
views.py
#login_required
def creaScheda(request):
if request.method == "POST":
form = CreaSchedaForm(request.POST)
if form.is_valid():
scheda = form.save(commit = False)
scheda.utente = request.user
scheda.save()
else:
form = CreaSchedaForm()
context = {"form": form}
return render(request, "crea_scheda.html", context)

Using instance variable inside a django form

I initialize a form using an instance of a model in my view like this
entry_form = EntryDetailsForm(instance=entry)
my model form is the following
class EntryDetailsForm(ModelForm):
start_date = forms.DateField(widget=TextInput(attrs={'class': 'form-control input-sm','readonly':''}))
start_hour = forms.TimeField(widget=TimeInput(attrs={'class': 'form-control input-sm input-append date', 'id':'starthour','readonly':''}))
end_hour = forms.TimeField(widget=TextInput(attrs={'class': 'form-control input-sm',
'id':'endhour','readonly':''}))
error_css_class = 'has_error'
required_css_class = 'has_warning'
def __init__(self, *args, **kwargs):
self.fields['start_date'] = self.instance.start.date()
self.fields['start_hour'] = self.instance.start.time()
self.fields['end_hour'] = self.instance.end.time()
class Meta:
model = Entry
exclude = ('start', 'end', 'creator')
widgets = {
'reason':Textarea(attrs={'class':'form-control input-sm'}),
'title': TextInput(attrs={'class': 'form-control input-sm'}),
'comment':Textarea(attrs={'class': 'form-control input-sm'}),
'patient': Select(attrs={'class':'form-control input-sm selectpicker',
'data-live-search':'true'}),
'event_category': Select(attrs={'class':'form-control input-sm'}),
'doctor': Select(attrs={'class': 'form-control input-sm selectpicker',
'data-live-search':'true'})
}
def save(self, commit=True):
print 'Form save method'
model = super(EntryDetailsForm, self).save(commit=False)
model.start = datetime.combine(self.cleaned_data['start_date'], self.cleaned_data['start_hour'])
model.end = datetime.combine(self.cleaned_data['start_date'], self.cleaned_data['end_hour'])
But I get an error that my EntryDetailsForm object doesn't have an instance attribute. Am I doing something wrong?
EDIT Using this method won't populate the value of start_date start_hour and end_hour fields. How do I do that in __init__?
EDIT2: I used the initial parameter in my view
entry_form = EntryDetailsForm(instance=entry, initial={'start_date':...etc})
and worked. Is there a way to do using the init?
You need to call super(EntryDetailsForm, self).__init__(*args, **kwargs), before self.instance usage.

Django form and formset - check if any has users and raise error if not

I have an application which has a form in which you can specify users by name or by formset where you select groups of users or both.
I need to add some sort of validation where the form won't proceed when sum of users selected manually and/or from groups will equal 0.
How do I make a communication between form and formset to raise validation error?
Is there another way of raising an error like in typical form and formset?
def form_valid(self, form):
context = self.get_context_data()
formset = context.get('formset')
is_valid = form.is_valid()
if formset:
is_valid &= formset.is_valid()
if not is_valid:
context['form'] = form
return self.render_to_response(context)
task = form.save()
if self.has_formset():
added_groups_list = []
for formset_form in formset:
...do something...
class TaskForm(forms.ModelForm):
sms_preview = forms.CharField(
label=u"Treść wiadomości SMS", required=False, widget=forms.Textarea(attrs={
'cols': 2,
'rows': 2,
'readonly': 'readonly',
})
)
users = forms.ModelMultipleChoiceField(User.objects.none(), label=u'Użytkownicy', required=False)
class Meta:
model = Task
fields = ('procedure', 'priority', 'location', 'message', 'date_from', 'date_to',
'users')
widgets = {
'date_from': DateTimePickerInput,
'date_to': DateTimePickerInput,
'procedure': forms.Select(attrs={'class': 'chosen',
"data-placeholder": u"Wybierz procedurę",
}),
'message': forms.Textarea(attrs={'cols': 2, 'rows': 2,
'placeholder': u"Własna treść wiadomości (opcjonalnie)"
}),
'hours': forms.TextInput(attrs={'placeholder': u'Godziny'}),
'minutes': forms.TextInput(attrs={'placeholder': u'Minuty'}),
}
def __init__(self, *args, **kwargs):
self.site = kwargs.pop('site')
self.procedure = kwargs.pop('procedure', None)
self.user = kwargs.pop('user', None)
super(TaskForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
layout = Layout('procedure')
date_from = timezone.now() + timezone.timedelta(hours=1)
self.fields['date_from'].initial = date_from.strftime("%d.%m.%Y %H:%M")
date_to = date_from + timezone.timedelta(hours=1)
self.fields['date_to'].initial = date_to.strftime("%d.%m.%Y %H:%M")
self.fields['date_from'].required = True
self.fields['date_to'].required = True
self.fields['message'].label = ""
self.fields['users'].widget.attrs = {
'class': 'chosen',
'data-placeholder': u'Nie wybrano użytkowników.',
'readonly': 'readonly',
'disabled': 'disabled',
}
class TaskActionGroupFormset(forms.formsets.BaseFormSet):
def __init__(self, *args, **kwargs):
self.site = kwargs.pop('site')
self.procedure = kwargs.pop('procedure', None)
super(TaskActionGroupFormset, self).__init__(*args, **kwargs)
#cached_property
def forms(self):
return [self._construct_form(i, site=self.site, procedure=self.procedure)
for i in xrange(self.total_form_count())]
#property
def empty_form(self):
form = self.form(
auto_id=self.auto_id,
prefix=self.add_prefix('__prefix__'),
empty_permitted=True,
site=self.site,
)
self.add_fields(form, None)
return form
Since you are using Django forms, you'll want to use the clean() method and override it to run your validation.