django-views

Querying Many to many fields in django template

与世无争的帅哥 提交于 2019-11-30 13:21:45
问题 This may not be relevant but just wanted to ask, IF an object is passed from views to template and in the template will i be able to query many to many fields Models code: class Info(models.Model): xls_answer = models.TextField(null=True,blank=True) class Upload(models.Model): access = models.IntegerField() info = models.ManyToManyField(Info) time = models.CharField(max_length=8, null=True,blank=True) error_flag = models.IntegerField() def __unicode__(self): return self.access Views: // obj

django date filter gte and lte

本小妞迷上赌 提交于 2019-11-30 13:21:41
问题 I need to find data within a certain set of parameters I am building a small booking system, that lets user see what vehicles are available for booking for their little safari trip. The system has bookings that have been entered previously or made previously by a client. If a booking's pickup_date = 2011-03-01 and dropoff_date = 2011-03-15 and I run a query with pickup=2011-03-09 and dropoff=2011-03-14 in my views as below, it doesn't return any results to see if a booking within that

How to create password input field in django

泄露秘密 提交于 2019-11-30 12:23:27
问题 Hi I am using the django model class with some field and a password field. Instead of displaying regular plain text I want to display password input. I created a model class like this: class UserForm(ModelForm): class Meta: password = forms.CharField(widget=forms.PasswordInput) model = User widgets = { 'password': forms.PasswordInput(), } But i am getting the following error: NameError: name 'forms' is not defined. I am using django version 1.4.0. I followed this link : Django password

Provide extra context to all views

*爱你&永不变心* 提交于 2019-11-30 12:18:22
I'm putting together a project management website for my team using django. My base template includes a sidebar menu which contains a list of all projects and users, linking to a DetailView for that user or project, respectively. My problem is that I need to provide the User and Project models to every view so that I can render that sidebar. I know how to add extra context ; the problem is that I feel like I'm violating DRY by modifying the context at each level. Is it possible to simply redefine the base TemplateClass so that all child classes— ListView , DetailView , etc.—contain the

Extending Django's Generic Views

怎甘沉沦 提交于 2019-11-30 12:13:00
I'm writing my first app in Django, and I have a problem with the create_object Generic View; In my urls.py , I have: (r'^new$', CreateView.as_view()), The problem is that when the user submits the "new" form, I need to manipulate the data that will be saved to the database (I actually need to add the user_id foreign key); without Generic Views I used to write: form = ClientForm(request.POST) if form.is_valid(): data = form.save(commit=False) data.user = request.user data.save() form.save_m2m() in my view (notice data.user=request.user ). I've searched Django docs but I can't find a way to do

Django formset set current user

倖福魔咒の 提交于 2019-11-30 12:06:05
问题 Related to this question, but expanding on it - How would I use this technique in a formset? I'd like to use the current logged in user in a form, but I'm using the form in a formset. The referenced solution for a single form is to pass request.user to the form and process in init. How do I add to the kwargs for each form in the formset? Example in my code: in forms.py class NewStudentForm (forms.Form): username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$', help_text

Django request Post json

穿精又带淫゛_ 提交于 2019-11-30 11:32:57
I try to test a view, I receive a json request from the IPad, the format is: req = {"custom_decks": [ { "deck_name": "deck_test", "updates_last_applied": "1406217357", "created_date": 1406217380, "slide_section_ids": [ 1 ], "deck_id": 1 } ], "custom_decks_to_delete": [] } I checked this in jsonlint and it passed. I post the req via: response = self.client.post('/library/api/6.0/user/'+ uuid + '/store_custom_dec/',content_type='application/json', data=req) The view return "creation_success": false The problem is the post method in view doesn't find the key custom_decks. QueryDict: {u'{"custom

Union and Intersect in Django

风流意气都作罢 提交于 2019-11-30 11:20:06
问题 class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple models just to ask my question. I wonder how can i query blogs using tags in two different ways. Blog entries that are tagged with "tag1" or "tag2": Blog.objects.filter(tags_in=[1,2]).distinct() Blog objects that are tagged with "tag1" and "tag2" : ? Blog objects that are tagged with exactly "tag1" and "tag2" and nothing

Django HttpResponseRedirect

痴心易碎 提交于 2019-11-30 11:18:47
I have created a basic contact form, and when the user submits information, it should redirect to the "Thank You" page. views.py : def contact(request): # if no errors... return HttpResponseRedirect('/thanks/') urls.py : (r'^contact/$', contact), (r'^contact/thanks/$', contact_thanks), Both pages work at the hard-coded URL. However, when I submit the form on /contact/ it redirects to /contact (no ending slash), which is a nonexistent page (either a 404 or an error page telling me I need a slash). What is the reason it not correctly redirecting, and how can I fix this? UPDATE: the return

django modelformset_factory sustains the previously submitted data even after successfully created the objects

十年热恋 提交于 2019-11-30 10:31:19
I am using django modelformset_factory in one of my view. I am using javascript for adding new forms to the formset in Template. Everything is working fine but my problem is that when i try to create a new object using modelformset_factory it shows me all the objects as forms which i previously created. Or in simple words it shows me the last submitted forms when i create a new instance using modelformset_factory forms. Like I have defined extra = 0 while initializing the modelformset_factory and from template I added 5 forms to formset and submitted those. Next time when I will re render the