django-views

How do I map one models objects to another models objects in a view

拜拜、爱过 提交于 2019-12-12 04:24:49
问题 I have a bunch of message records that I would like to assign to different taskboxes. #models.py class TaskBox(models.Model): name = models.CharField(max_length=64, blank=False) def __str__(self): return u'%s' % (self.name) class Admin: pass class InboxEntry(models.Model): job_number = models.CharField(max_length=14, unique=False, blank=False, null=False) job_name = models.CharField(max_length=64, unique=False, blank=False, null=False) request = models.CharField(max_length=64, choices=PRINT

How to use render_to_response to render a html page within div in Django? [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 04:17:33
问题 This question already has an answer here : How to access json objects in the template when HttpResponse is used in django view? (1 answer) Closed 3 years ago . I have a view which returns data to template using render_to_response function as below - @page_template("app/jsComp.html") # just add this decorator def jsComp(request, template="app/jsComp.html", extra_context=None): basic_v_obj = js_basicinfo.objects.get(cid = request.session.get('id')) # Sends json data objects in context container

Django: Set initial Value with a Passed Variable

一世执手 提交于 2019-12-12 04:12:16
问题 I am using Django and want to pass a variable from a view to set an initial value within a form. I have the following view: def change_chosenCharity(request): charityid=12 form = updateCharity(charityid) return render(request, 'meta/changechosencharity.html', {'form': form}) With the following form: class updateCharity(BootstrapForm): currentCharities = forms.ModelChoiceField(queryset=Charity.objects.filter(enabled=1), empty_label=None,widget=forms.Select(attrs={"class": "select-format"}))

django upload image file from templates

99封情书 提交于 2019-12-12 04:11:32
问题 every one ,,I can upload image from admin site,, but do not know how to upload image from the templates,, models.py from django.db import models from django.contrib.auth.models import User from PIL import Image from django.utils import timezone def get_imagep_Product(instance, filename): return '/'.join(['products', instance.slug, filename]) class ProductsTbl(models.Model): model_number = models.CharField(max_length=255, blank=True, null=True) name = models.CharField(max_length=255, blank

Is there a way to add custom code to login_required

岁酱吖の 提交于 2019-12-12 04:04:47
问题 I've made my own LoginRequiredMixin like this: class LoginRequiredMixin(object): @classmethod def as_view(cls, **initkwargs): view = super(LoginRequiredMixin, cls).as_view(**initkwargs) # (!!) multilangue = reverse_lazy, PAS reverse return login_required(view, login_url=reverse_lazy('my_home_login')) So far so good, everything works fine when you create new views like this: class EditView(LoginRequiredMixin, generic.UpdateView): model = Personne template_name = 'my_home/profile/base.html'

how to serve Result file using Django?

让人想犯罪 __ 提交于 2019-12-12 04:01:15
问题 I have developed an App that takes input file from the upload method without using model and run some code behind the server like this: /MyDjango_project_directory/media/input.csv And generates some result files in a location like this. /Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.csv /Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.png Presently I am just moving the output files into "media" folder through "sys" command in

Django: how to refresh or reload models from database

拥有回忆 提交于 2019-12-12 03:58:01
问题 Some of my tables in database are periodicity updated by several python scripts outside of Django. As a result, the Django's views are not aware of the latest data in database and is showing the old data. I have tried many suggestions online but nothing work except called connection.close() before using the model. Here are the approaches I have tried, nothing worked. from django.views.decorators.cache import never_cache @never_cache # <===== def GetData(request): data = Table.objects.get(id=1

How to pass context from view to action in html file in django?

只谈情不闲聊 提交于 2019-12-12 03:42:49
问题 I am passing context from view.py file def change_password(request,pk=None): user = MyUser.objects.get(pk=pk) if request.method == "POST": form = PasswordChangeForm(user=user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return render(request, "nurse/change_password.html", {"user_id":user.id}) // passing user_id as context I can see this value in my html file <div class="form-group"> <label class="control-label allign col-sm-3" > {{user_id}}

Prevent users to access data of another user when typing the slug in the url

孤人 提交于 2019-12-12 03:39:11
问题 If user 1 creat this ticket : mywebsite/manager/tickets/ticket-from-user-1/ And user 2 create that : mywebsite/manager/tickets/ticket-from-user-2/ How can I prevent user 1 to access the ticket from user 2 or other users by typing it in the url? views.py class TicketDisplay(LoginRequiredMixin, DetailView): model = Ticket template_name = 'ticket_detail.html' context_object_name = 'ticket' slug_field = 'slug' def get_context_data(self, **kwargs): context = super(TicketDisplay, self).get_context

HTTP Request Object does not respond

丶灬走出姿态 提交于 2019-12-12 03:36:49
问题 I am making a login form in Django. When I am click on login submit button , It gives an error of ValueError at /registration/ The view registration.views.login didn't return an HttpResponse object. The view file is from django.template import loader from django.shortcuts import render from registration.models import Registration from django.http import HttpResponse def login(request): if request.method == 'POST': user = authenticate(username=request.POST['username'], password=request.POST[