django-views

How to pass data between django views

限于喜欢 提交于 2019-12-04 11:12:00
问题 This questions addresses my question genearally, but I am looking for a more specific explanation. I would like a user to update a a group of model objects, however, the queryset for these objects will need to be retrieved first. My plan is to do this in two seperate URs/views, getting the query set info from the first, then displaying the model formset to be updated next. My first view gives a list of all the the "Project"s (One of my models), and retrieves the id of the project selected.

Django - Redirect with context

一笑奈何 提交于 2019-12-04 11:10:31
I have a Sign Up page which takes a user's username and password and saves it in the database. If the user is successfully signed up, I want to redirect him to a 'Sign In' page, with a value denoting that Signup was successful. Expected output on the redirected page: You have been successfully signed up. Sign In Username : --- Password : --- So, I am redirecting from the signup to the signin page, but I want to pass a value showing that the user has come from the Signup page and is successfully signed up. How do I perform this? Use the messages framework to add a message in the first view and

How to redirect users to a specific url after registration in django registration?

爷,独闯天下 提交于 2019-12-04 10:51:52
问题 So I am using django-registration app to implement a user registration page for my site. I used Django's backends.simple views which allows the users to immediately login after registration. My question is how do I redirect them to my other app's page located in the same directory as the project. Here is what my main urls.py looks like: from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin

Adding custom validation to a field, for the generic view CreateView

孤人 提交于 2019-12-04 10:46:29
The problem Add custom validation to a form field in Django 1.3, the form is created by the generic view CreateView. The model class Picture(models.Model): file = models.ImageField(upload_to=get_image_path) filename = models.CharField(max_length=50, blank=True) user = models.ForeignKey(User, editable=False) upload_date = models.DateTimeField(auto_now_add=True,editable=False) Generic view CreateView, a bit modified class PictureCreateView(CreateView): model = Picture def clean_file(self,form): if image: if image._size > settings.MAX_IMAGE_SIZE: raise ValidationError("Image file too large ( >

How to use Django ORM to get a list by year of all articles with an article count

白昼怎懂夜的黑 提交于 2019-12-04 10:09:08
I am trying use the django ORM to get a list by year of all my articles with an article count beside it, such as this: 2010 (5 articles) 2009 (4 articles) 2008 (9 articles) I have tried things such as: archive = Articles.objects.dates('created', 'year').annotate(archive_count=Count('created')) or: archive = Articles.objects.values('created').annotate(archive_count=Count('created')) or: archive = Articles.objects.values('created').aggregate(archive_count=Count('created')) The last one gave me the right count but didn't give me any of the year values, the other ones give a mix of either nothing

Can I create sub domain for each user in Django

别说谁变了你拦得住时间么 提交于 2019-12-04 10:08:43
I want users create their own account and users should get their own like user.foo.com and different point to different template folder. Is it possible in Django. I quite new to Django Django itself is unaware of whatever web server you are using. Usually, to configure extra sub-domains, you need to add virtual hosts to your webserver's configuration and make sure your DNS provider forwards requests for that sub-domain to the appropriate server. To do this directly, you'll need to run Django under a user account that has write access to your webserver's configuration file, which is really not

Converting a function based view to a class based view with only a form and no model (object)

孤街浪徒 提交于 2019-12-04 10:06:29
Right now, this is how the password is changed within a user profile. What is the best way of converting this to a class based view knowing that there is no model involved? This is the view for changing the password @login_required def profile_change_password(request): """ Change password of user. """ user = get_object_or_404(User, username__iexact=request.user.username) if request.method == 'POST': form = PasswordChangeFormPrivate(user=user, data=request.POST) if form.is_valid(): form.save() messages.add_message (request, messages.INFO, _('password changed')) return HttpResponseRedirect

dynamic file upload path with current instance id

杀马特。学长 韩版系。学妹 提交于 2019-12-04 09:03:34
I have a form which gets current logged in user, some inputs and a file: class AddItemForm(ModelForm): class Meta: model = Item exclude = ['user'] For this form a have a view: item_form = AddItemForm(request.POST, request.FILES) if item_form.is_valid(): item = item_form.save(commit=False) item.user = request.user item.save() for this item's file field i am using upload_to feature. here is my modal: class Item(models.Model): user = models.ForeignKey(User) cover_image = models.FileField(upload_to=get_upload_path) def get_upload_path(instance, filename): return "items/user_{user_id}/item_{item_id

How to specify the login_required redirect url in django?

亡梦爱人 提交于 2019-12-04 08:13:27
问题 I have a view function: @login_required def myview(): # do something # respond something pass How can I specify the exact URL for this view function to be redirected? 回答1: LOGIN_URL in your settings Reference: LOGIN_URL LOGIN_REDIRECT_URL 回答2: you can do this in your view works fine for me without declaring in settings.py from django.contrib.auth.decorators import login_required @login_required(login_url='/example url you want redirect/') #redirect when user is not logged in def myview

Saving Hashed Version of User Password in Django Form Not Working

北城以北 提交于 2019-12-04 08:10:36
I have been trying to save the hashed version of a user password but it's not working. forms.py : class up_form(forms.ModelForm): class Meta: model = Users fields =['email', 'password', 'username', 'status'] views.py : from myapp.forms import up_form from django.contrib.auth.hashers import make_password def register(request): if request.method == 'POST': sign_up = up_form(request.POST or None) if sign_up.is_valid(): sign_up.password = make_password(sign_up.cleaned_data['password']) sign_up = sign_up.save(commit = False) sign_up.status = 1 sign_up.save() But my password still get saved in plain