Django. Pass variable from view to template - django

I want to pass variable appuser to template and I don't understand how to do it.
I have tried to use kwargs.update but it still doesn't work.
I have a view:
class CausesView(AjaxFormView):
appuser = None
causes = []
cause_allocation_set = None
def prepare_request(self, request, *args, **kwargs):
self.causes = Cause.objects.filter(is_main_cause = True)
self.appuser = AppUser.get_login_user(request)
self.cause_allocation_set = set([r.cause_id for r in self.appuser.current_cause_save_point.cause_allocations_list])
def prepare_context(self, request, context, initial):
initial.update(
causes = self.cause_allocation_set,
appuser = self.appuser,
)
def prepare_form(self, request, form):
form._set_choices("causes", [(r.id, r.title) for r in self.causes])
def custom_context_data(self, request, **kwargs):
kwargs.update(
special_test = "dsf"
)
return kwargs
def process_form(self, request, form):
data = form.cleaned_data
try:
with transaction.atomic():
if self.cause_allocation_set != set(data.get('causes')):
self.appuser.save_causes(data.get('causes'))
except Exception as e:
message = "There was an error with saving the data: " + str(e.args)
return AjaxErrorResponse({'title':"Error", 'message':message})
return AjaxSuccessResponse('Causes Saved')
And I have a form:
class CauseForm(AjaxForm):
causes = forms.TypedMultipleChoiceField(label="Select Causes", choices = (), required = False, coerce = int,
widget = forms.CheckboxSelectMultiple())
def clean(self):
cleaned_data = super(CauseForm, self).clean()
causes = cleaned_data.get('causes')
validation_errors = []
if not causes is None and not len(causes):
validation_errors.append(forms.ValidationError("At least one Cause is required"))
if len(validation_errors):
raise forms.ValidationError(validation_errors)
return cleaned_data
How can I get variable appuser in temlpate?
For example:
{{ appuser.name }}
doesn't work.

Read How to use get_context_data in django
and
https://docs.djangoproject.com/en/1.9/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_context_data
Here is example of how you can do this
class CausesView(AjaxFormView):
...
def get_context_data(self, **kwargs):
context_data = super(CausesView, self).get_context_data(**kwargs)
context_data['appuser'] = self.appuser
return context_data

Related

prepoluate a generec createview

i want that a form is prepoluate with data
my model:
TYPE = (("S",'Swing'),
("R","Rapide"))
class valuation(models.Model):
stock = models.ForeignKey("stock",on_delete=models.CASCADE,related_name='valuation',)
date = models.DateField(auto_created=True)
val_type = models.CharField(choices=TYPE, max_length=1,default='R')
user = models.ForeignKey("users.User", on_delete=models.CASCADE)
def __str__(self):
return f"{self.stock} - {self.date} - {self.val_type}"
my view:
class valuationCreateviewSwing(CreateView):
template_name = "evaluation/evaluation_create.html"
form_class = valuationModeform
def get_form_kwargs(self): # prepopulate form
kwargs = super(valuationCreateviewSwing, self).get_form_kwargs()
stck = get_object_or_404(stock, pk=self.kwargs['pk'])
kwargs['user'] = self.request.user
kwargs['val_type'] = "S"
kwargs['stock'] = stck
return kwargs
def get_context_data(self, **kwargs):
# we need to overwrite get_context_data
# to make sure that our formset is rendered
data = super().get_context_data(**kwargs)
if self.request.POST:
data["val_detail"] = ChildFormset1(self.request.POST)
else:
data["val_detail"] = ChildFormset1()
data.update({
"typeVal": "Swing",})
return data
def form_valid(self, form):
context = self.get_context_data()
val_detail_Swing = context["val_detail_Swing"]
self.object = form.save(commit=False)
# add data info neede about valuation model
self.object = form.save()
if val_detail_Swing.is_valid():
val_detail_Swing.instance = self.object
val_detail_Swing.save()
return super().form_valid(form)
def get_success_url(self):
return reverse("stock:stock-list")
I've a child form in my view (this part works ok):
ChildFormset1 = inlineformset_factory(
valuation, val_detail_Swing, form=valuationSwingModelform, can_delete=False)
I tried to use ge_for_kwargs but it seems not working as I've an error message :
init() got an unexpected keyword argument 'user'
You can use get_initial() method:
class valuationCreateviewSwing(CreateView):
template_name = "evaluation/evaluation_create.html"
form_class = valuationModeform
def get_initial(self):
query = self.request.GET
return {
'user': self.request.user.pk
'val_type': "S",
'stock': self.kwargs.get('pk')
}
...
Or you should override __init__() method and stay to use get_form_kwargs()
class valuationModeform(ModelForm):
class Meta:
model = Valuation
fields = '__all__'
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
val_type = kwargs('val_type', None)
stock = kwargs.pop('stock', None)
super().__init__(*args, **kwargs)
# assign initial values
self.fields['user'].initial = user
self.fields['val_type'].initial = val_type
self.fields['stock'].initial = stock

super(type, obj): obj must be an instance or subtype of type Django

I work on a small Django App and get an error tells me: super(type, obj): obj must be an instance or subtype of type.I am trying to save the details of the sale in the database but I get this error.
Views
class VentaCreateView(LoginRequiredMixin, ValidatePermissionRequiredMixin, CreateView):
model = Venta
form_class = nueva_venta_form
template_name = 'venta/venta_form.html'
success_url = reverse_lazy('Index')
permission_required = 'store_project_app.change_categoria'
url_redirect = success_url
#method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
data = {}
try:
action = request.POST['action']
if action == 'autocomplete':
elif action == 'add':
#ventas = request.POST['action']
#ventas = request.POST['ventas']
ventas = json.loads(request.POST['ventas'])
#print(ventas)
venta = Venta()
venta.id_cliente = Cliente.objects.get(id_cliente = ventas['id_cliente'])
venta.id_empleado = Empleado.objects.get(id_empleado = ventas['id_empleado'])
venta.fecha_venta = ventas['fecha_venta']
venta.forma_pago = Metodo_Pago.objects.get(id_metodo_pago = ventas['forma_pago'])
venta.precio_total = float(ventas['precio_total'])
venta.save()
for i in ventas['productos']:
detalle_venta = Detalle_Venta()
detalle_venta.id_venta = venta.id_venta
detalle_venta.id_producto = i['id_producto']
detalle_venta.cantidad = int(i['cantidad'])
detalle_venta.subtotal = float(i['subtotal'])
detalle_venta.save()
else:
data['error'] = 'No ha ingresado a ninguna opciĆ³n'
except Exception as e:
data['error'] = str(e)
return JsonResponse(data, safe=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Crear una Venta'
context['entity'] = 'Venta'
context['list_url'] = self.success_url
context['action'] = 'add'
return context

form for formset object has no attribute 'cleaned deta'

I have genericview which handle 3 difference form(2 formsets), when i try check cleaned_data, i have error, but when i try make the same by debugger i didn't see anything problem, who may know why i can't make it?
My View :
class CompanyCreateView(LoginRequiredMixin, CreateView):
model = Company
template_name = 'main/company/create_page.html'
form_class = CompanyCreateForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['page'] = Page.objects.active().filter(slug='add_company').first()
if self.request.POST:
context['images'] = ImageGalleryFormSet(self.request.POST, self.request.FILES)
context['company_locations'] = CompanyLocationFormset(self.request.POST)
else:
context['images'] = ImageGalleryFormSet(self.request.GET or None)
context['company_locations'] = CompanyLocationFormset(self.request.GET or None)
return context
#transaction.atomic
def form_valid(self, form):
context = self.get_context_data()
images = context['images']
company_locations = context['company_locations']
self.object = form.save()
self.object.active = False
self.object.status = CompanyConstants.CompanyStatus.NEW
self.object.owner = self.request.user
self.object.save()
for image in range(len(images)):
key_image = 'gallery_items-' + str(image) + '-image'
form_image = self.request.FILES.get(key_image)
if form_image:
ImageGallery(image=form_image, company_id=int(self.object.id)).save()
print(company_locations.forms[0].get_cleaned_data)
for location in range(len(company_locations)):
key_locations = 'form-' + str(location) + '-location'
key_address = 'form-' + str(location) + '-address'
if self.request.POST.get(key_locations):
location = self.request.POST.get(key_locations)
address = self.request.POST.get(key_address)
company = self.object
CompanyLocation.objects.create(location=clean_location(location), address=address, company=company)
return super().form_valid(form)
def get_success_url(self):
return reverse('main:profile', args=[self.request.user.username])
When i try print this i have error, this code that i have works correct but, he is bad.
MY FORM :
class CompanyLocationForm(forms.ModelForm):
location = geo_forms.PointField(widget=GooglePointFieldWidget)
class Meta:
model = CompanyLocation
fields = ('location', 'address')
CompanyLocationFormset = formset_factory(CompanyLocationForm, max_num=10)
you need to clean your data to check the problem
#transaction.atomic
def form_valid(self, form):
def clean(self):
all_clean_data = super().clean()
all_clean_data[['page'] = Page.objects.active().filter(slug='add_company').first()
...
So i see a lot off data Just test
def Test(request):
if request.method == "POST":
form =CompanyLocationForm(data=request.POST)
if form.is_valid():
print(company_locations.forms[0].get_cleaned_data)

why i am getting this error KeyError at /evaluationtest/? 'request'

hi i have create an Crud function for evaluation Test but i am getting keyerror 'request' this kind of strange to me i have not seen this error before i am new to django can somebody help me to fix it?
def validate(self, data, *args, **kwargs):
questions = self.context['request'].data.get("questions")
if not questions:
raise serializers.ValidationError("questions are required")
if self.context["request"].method == "POST":
self.questions = QuestionSerializer(data=questions, many=True)
self.questions.is_valid(raise_exception=True)
elif self.context["request"].method == "PUT":
self.questions = questions
self.new_questions = self.context["request"].data.get(
"new_questions")
if self.new_questions:
self.new_questions = QuestionSerializer(
data=self.new_questions, many=True)
self.new_questions.is_valid(raise_exception=True)
return data
def create(self, data):
evaluation_test = EvaluationTest()
evaluation_test.category = data['category']
evaluation_test.admin = data['admin']
evaluation_test.title = data['title']
evaluation_test.type = data['type']
evaluation_test.save()
for question in data['questions']:
question.evaluationtest = evaluation_test
question.save()
return evaluation_test
def update(self, instance, validated_data):
instance.title = validated_data.get["title", instance.title]
instance.type = validated_data.get["type", instance.type]
instance.category = validated_data.get["category", instance.category]
instance.admin = validated_data.get["admin", instance.admin]
for question in self.questions:
q = QuestionSerializer(instance=question["id"], data=question)
q.is_valid(raise_exception=True)
q.save()
if self.new_questions:
new_questions = self.new_questions.save()
for question in new_questions:
question.save()
return instance
views.py
Here is my view.py code when i am implementing the whole crud application you can see and please give me how can i modify to make it work
class EvaluationTestViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated, )
serializer_class = EvaluationTestSerializer
queryset = EvaluationTest.objects.all()
#action(methods=['get'], detail=False, url_path='by-category/(?P<category_id>\d+)', url_name='by_category')
def by_category(self, request, *args, **kwargs):
evaluationtest = EvaluationTestSerializer.by_category(
kwargs['category_id'])
if evaluationtest:
return Response(evaluationtest)
return Response(status=HTTP_404_NOT_FOUND)
def create(self, request):
serializer = EvaluationTestSerializer(data=request.data)
if serializer.is_valid():
evaluationtest = serializer.create(request)
print(evaluationtest)
if evaluationtest:
return Response(status=HTTP_201_CREATED)
return Response(status=HTTP_400_BAD_REQUEST)
you have to set the request in the serializer context.
change your create method:
def create(self, request):
serializer = EvaluationTestSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
# call save() instead of create
evaluationtest = serializer.save()
print(evaluationtest)
# ideally you should also return serializer.data
return Response(status=HTTP_201_CREATED)
# ideally you should also return serializer.errors
return Response(status=HTTP_400_BAD_REQUEST)

Access missing value in form.cleaned_data

I was trying to dynamically generate fields as shown in http://jacobian.org/writing/dynamic-form-generation/. My case slightly differs in that I am looking to use multiplechoicefield that is dynamically created. This is what I came up with...
views.py
def browseget(request):
success = False
if request.method == 'POST':
list_form = ListForm(request.POST)
if list_form.is_valid():
success = True
path = list_form.cleaned_data['path']
minimum_size = list_form.cleaned_data['minimum_size']
follow_link = list_form.cleaned_data['follow_link']
checkboxes = list_form.cleaned_data['checkboxes']
....do something
else:
list_form = ListForm(name_list)
ctx = {'success': success, 'list_form': list_form, 'path': path, 'minimum_size': minimum_size}
return render_to_response('photoget/browseget.html', ctx, context_instance=RequestContext(request))
forms.py
class ListForm(forms.Form):
path = forms.CharField(required=False)
minimum_size = forms.ChoiceField(choices=size_choices)
follow_link = forms.BooleanField(required=False, initial=True)
def __init__(self, *args, **kwargs):
name_list = kwargs.pop('name_list', None)
super(ListForm, self).__init__(*args, **kwargs)
print 'Received data:', self.data
if name_list:
name_choices = [(u, u) for u in name_list]
self.fields['checkboxes'] = forms.MultipleChoiceField(required=False, label='Select Name(s):', widget=forms.CheckboxSelectMultiple(), choices=name_choices)
def clean_path(self):
cd = self.cleaned_data
path = cd.get('path')
if path == '': path = None
return path
def clean_minimum_size(self):
cd = self.cleaned_data
minimum_size = cd.get('minimum_size')
if minimum_size is None: minimum_size = 0
return int(minimum_size)
The form generates and displays perfectly... until I post some data. The 'checkboxes' field doesn't show up in list_form.cleaned_data.items() while it shows in self.data. As it is the form breaks with a KeyError exception. So Im asking, how do i access the checkboxes data?
You're not passing in the name_list parameter when you re-instantiate the form on POST, so the field is not created because if name_list is False.