django-views

How to load an instance in Django ModelForms

回眸只為那壹抹淺笑 提交于 2019-12-04 02:04:16
问题 I am using Django's user model. How do I get a Django ModelForm to prepopulate values in a template? I know I have to use the instance for that form, but where am I going wrong below: models.py: class Site(models.Model): user = models.ForeignKey(User, ) site_name = models.CharField(max_length=128, blank=False, null=False) forms.py: class SiteForm(forms.ModelForm): class Meta: model = Site fields = '__all__' views.py: def settings(request): site_profile = Site.objects.get(user=request.user) if

Queryset for current logged in user Django

僤鯓⒐⒋嵵緔 提交于 2019-12-04 01:55:51
问题 I am doing queryset with my model. Now the queryset is displaying all the data in my html page. But I want to display only the logged in users data. models.py class Data(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) Status = models.CharField(max_length=10, blank=False) Date = models.DateField(blank=True, null=True) views.py @login_required def search(request): status_list = Data.objects.all() status_filter = UserFilter(request.GET, queryset=status_list) return

Django: Redirect to the previous page and keep the scroll position

十年热恋 提交于 2019-12-04 01:52:35
问题 I have seen this question where you can redirect to the previous page using: return HttpResponseRedirect(request.META.get('HTTP_REFERER')) but is there a way to also keep the scroll position that it was at? The above reloads the page afresh. 回答1: After some back and forth, this is what I came up with (with help from this answer): from django.http import HttpResponse def myview (request): .. .. return HttpResponse('<script>history.back();</script>') Also, keep in mind that history.back() does

What does it mean for an object to be unscriptable?

青春壹個敷衍的年華 提交于 2019-12-04 00:35:58
I don't know what's going on here... I just want to check the value of a model field and then update it accordingly... any help or insight is appreciated! model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) beta = models.CharField(max_length=1, blank=True, null=True) view: from internal.accounts.models import UserProfile from django.contrib.auth.models import User @login_required def beta_testers(request): user = User.objects.get(username=request.user.username) user_profile = user.get_profile() count = UserProfile.objects.filter(beta='1').count() if count < 50

Restrict `UpdateView` dataset for authenticated user in Class Based Views

倾然丶 夕夏残阳落幕 提交于 2019-12-03 22:46:07
I have a Django project where I extended the User to have a Profile using a OneToOneField. I'm using CBV UpdateView which allows users to update their profile. The URL they visit for this is ../profile/ user /update. The issue I have is that if a user types in another users name they can edit the other persons profile. How can I restrict the UpdateView so the authenticated user can only update their profile. I was trying to do something to make sure user.get_username == profile.user but having no luck. Models.py from django.db import models from django.contrib.auth.models import User from

Getting all items less than a month old

倾然丶 夕夏残阳落幕 提交于 2019-12-03 22:26:31
Is there a way to get all objects with a date less than a month ago in django. Something like: items = Item.objects.filter(less than a month old).order_by(...) What is your definition of a "month"? 30 days? 31 days? Past that, this should do it: from datetime import datetime, timedelta last_month = datetime.today() - timedelta(days=30) items = Item.objects.filter(my_date__gte=last_month).order_by(...) Takes advantange of the gte field lookup. items = Item.objects.filter(created_date__gte=aMonthAgo) Where aMonthAgo would be calculated by datetime and timedelta. 来源: https://stackoverflow.com

Django HttpResponseRedirect vs render_to_response - how to get a login form to behave the way I need it to

谁都会走 提交于 2019-12-03 21:55:34
I've already checked out the following stackoverflow question regarding the difference between HttpResponse, HttpResponseRedirect, and render_to_response , as well as having gone through the official django docs, but I'm really uncertain how best to get the functionality I'm looking to create. Right now I have an index.html with a login function (as seen in the views.py below) where the render_to_response that brings me to portal/index.html . However, as urls.py (see below) dictates, the url in the url bar of my browser is http://127.0.0.1:8000/login/ . This means that refreshing the page

Django: ajax response for valid/available username/email during registration

情到浓时终转凉″ 提交于 2019-12-03 21:41:27
I am using jQuery to do some inline form validation during user registration to prevent form errors after posting by checking to see if: username is available email has not already been registered The idea is to give the user feedback before the form is submitted to prevent frustration. The code is at the bottom. Questions: Is this a potential security problem? I had the thought that someone looking at my javascript could find the url I am polling for the username/email confirmation and then use it themselves (I don't know why they would do this, but one never knows). If it is, what

How to validate mulitple forms in a single formView class Django

萝らか妹 提交于 2019-12-03 21:24:57
I have a formView class as you can see below:- view.py class ThreadForm(FormView): template_name = 'thread.html' form_class = ThreadModelForm success_url = '/success' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. print form.cleaned_data return super(ThreadForm, self).form_valid(form) def get_context_data(self, **kwargs): context = super(ThreadForm, self).get_context_data(**kwargs) context['second_form'] = MessageModelForm return context thread.html {form.as_p} {second_form.as_p} SUBMIT In my template thread.html ,

Django Management Form is failing because 'form-TOTAL_FORMS' and 'form-INITIAL_FORMS' aren't correctly populated

£可爱£侵袭症+ 提交于 2019-12-03 21:18:33
Information: I would like to create nested forms as is best described through the example provided at: http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ The tutorial at this page seems to be pretty good && it is attempting to accomplish the exact problem I am encountering. There seems to be an issue with this implementation in the views.py file when there is no POST request data (I.e. we are performing the initial population from database). The code can be seen at the URL provided above (if needed I can post some of the code, but I fear it will take away from the information