django-views

Can I make a view using UpdateView and DeleteView together?

梦想的初衷 提交于 2019-12-10 11:55:49
问题 I am looking for some information about generic views in django. I want a page that will render a form for an object. On my page I want one submit button to update the object and another submit button that will delete the object. Is it a possible to do this using a single view? 回答1: Yes this is possible. It is possible create a view to handle update and delete functionality using both class based views and function based views. You will find it much easier using function based views. (I'm not

Django CBV: Programmatically save HTML page to PDF file on the server

╄→尐↘猪︶ㄣ 提交于 2019-12-10 11:47:18
问题 Visiting the relevant url, an html page is rendered. I need to programmatically save this html page to a pdf file on the server. views.py class PdfView(DetailView): model = TheModel template_name = 'pdf.html' urls.py urlpatterns = [ url( r'^(?P<pk>[0-9]+)/$', PdfDataView.as_view(), name='pdf-data', ), ] 回答1: Try an approach like below. Although it presents serving file to the end user via response, but modifying it to write file on server should be easy for you: The functional view: def pdf

Passing objects from template to view using Django

核能气质少年 提交于 2019-12-10 11:45:13
问题 I am trying to figure out the architecture for the following app: The user is presented with a table. Each table cell has several fields the user will be filling in. There is a general submit button: when clicked on all the input data (along with some calculated data per cell based on the input values) should pass to a Django view. Here are the following questions: Can I organize the data structure as a set of objects in a way that each object will correspond to a table cell, whereas the

Django urls.py and views.py behaviour -

谁都会走 提交于 2019-12-10 11:41:45
问题 I'm currently experimenting with Django and creating apps following the tutorials on the official website. So my urls.py looks like: urlpatterns = patterns('', (r'^/$','ulogin.views.index'), #why doesn't this work? (r'^ucode/$', 'ulogin.views.index'), (r'^ucode/(\d+)/$', 'ulogin.views.index'), ) And my views.py looks like: def index(request): return HttpResponse("Hello, world. You're at the poll index.") def redirect_to_index(request): return HttpResponseRedirect('/ucode/') When I run the

Django: form to query database

别说谁变了你拦得住时间么 提交于 2019-12-10 11:35:51
问题 I want a user to be able to perform the following query: Fetch all the people, without Phd, with full time contract with a contract within two dates. that translates in Django: Contract.objects.filter( person__is_doctor = False, type_contract = 'full', starting_date__gte = start_date, ending_date__lte = end_date ) How can I make a form/view/template to allow the user enter both start_date and end_date and show the results? models class Person(models.Model): name = models.CharField(max_length

How to pass data from one view to the next

*爱你&永不变心* 提交于 2019-12-10 11:29:14
问题 Summary: I am trying to build a job site. On index.html the user enters a zip code into a form to see jobs in that zip code, this form is handled with the job_query view. This brings them to another page(search.html) where at first you only see jobs in that specific zip code but I am trying to add a filter that lets the user see jobs within X miles. How can I pass the zip code value entered in the from on index.html to the next page? index.html: <h2>Find a Job</h2> <!--Search Bar--> <form

Load Django Form Object with data from Model instance already “loaded”

那年仲夏 提交于 2019-12-10 10:12:36
问题 I have the following type of template in Django: <form .... > <label class="control-label" for="id_quantity">Quantiy</label> {{ form.quantity }} <label class="control-label" for="id_title">Quantiy</label> {{ form.title}} </form> The view contains a ModelForm object in its context from which the "form" variable in the template is derived. Often, you can pass a form that is bound to data like request.POST. How do I load a form that is bound to the data of a specific instance of a model? Is

Class has no 'objects' member in django

懵懂的女人 提交于 2019-12-10 09:33:17
问题 from django.http import HttpResponse from .models import Destination def index(request): boards = Destination.objects.all() boards_names = list() for Destination in boards: boards_names.append(Destination.destinationtext) response_html = '<br>'.join(boards_names) return HttpResponse(response_html) I have written this code following just for practice of django framework but I am getting the following errors through pylint : E1101:Class 'Destination' has no 'objects' member E0601:Using variable

Viewset 'create' custom assign value in Django Rest Framework

雨燕双飞 提交于 2019-12-10 09:27:55
问题 Would like to set a CustomUser 's username by using the input email, but where to do the custom assigning, in view? At the same time it receiving a file as well. Models.py class CustomUser(AbstractUser): avatar = models.ImageField(max_length=None, upload_to='avatar', blank=True) Serializers.py class CustomUserSerializer(serializers.ModelSerializer): class Meta: model = CustomUser fields = ('id', 'first_name', 'last_name', 'email', 'password', 'avatar', 'groups') Views.py class

Hidden field in Django form not in cleaned_data

青春壹個敷衍的年華 提交于 2019-12-10 02:36:56
问题 I have this form: class CollaboratorForm(forms.Form): user = forms.CharField(label="Username",max_length=100) canvas = forms.IntegerField(widget=forms.HiddenInput) .... def clean_user(self): user = self.cleaned_data['user'] canvas = self.cleaned_data['canvas'] In the view I'm simply calling if form.is_valid(): I get the error: KeyError at /canvas/1/add-collaborator/ 'canvas' According to firebug the value is posting, it's just doesn't seem to be making it to my clean function. Am I doing it