django-views

Django serializer return partial data

半世苍凉 提交于 2019-12-13 00:03:50
问题 My code for receiving data and then returning the response is: serializer = VideoSerializer(data=data, partial=True) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400) Here if the serializer is valid I want to return partial data from the serializer(after removing some fields). It could be done if I create another serializer object and pass fields tuple to it as given here https://stackoverflow.com/a

Django created_at__gt=self.request.user.last_login workinly only on users already signed in.

强颜欢笑 提交于 2019-12-12 23:12:51
问题 Intro: I have a 3 models user , post , group . User is able to make posts however each post has to belong to a group. Users have to choose from the existing groups for their posts. Users cannot add, delete, update group's. Furthermore: Users can become a member of groups and when they click on a certain group. They see all the posts in that group. What I want When Users come on the home page they see posts that were added since the last time they logged in My Models class Post(models.Model):

i have a checkbox with multiple inputs (multiple x and y)

耗尽温柔 提交于 2019-12-12 22:34:55
问题 I am unable to save MEDIA_CHOICES in the database I don't know why ! The market parameter is saved. But I have problem with sector parameter that has MEDIA_CHOICES as input, The user choose the inputs but after save it is empty in the database. I cant find where is the problem. Image here the user need to check sector than save the value,when i access to the saved page i find the sector field empty not saved Admin page image here class Media(models.Model): Media_Choices= models.CharField(max

Why does Django Redirect Test Fail?

匆匆过客 提交于 2019-12-12 22:08:55
问题 I have a view unit test that is failing and I can't figure out the reason why. I believe it has something to do with the test database. The view in question is the default Django login view, django.contrib.auth.views.login. In my project, after the user logs in, they are redirected to a page that show which members are logged in. I've only stubbed out that page. Here is the unit test: from django.test import TestCase from django.contrib.auth.models import User from django.test.client import

Django views architecture

柔情痞子 提交于 2019-12-12 21:22:46
问题 I'm new to django and had a question regarding organizing views. manage.py startapp creates a views.py in my app folder. But django-admin.py startproject <name> does not create a corresponding views.py file in the <project_name>/<project_name> folder. I find it intuitive to have global views which do not correspond to a particular app. For example, a login page would and should be independent of any app that I create (its associated with the django auth app). So, would it make sense to create

Django - Global navigation in base.html using model

时光总嘲笑我的痴心妄想 提交于 2019-12-12 21:09:08
问题 I have a number of templates that extend base.html . I want the base.html template to house my global navigation and have the text and links in the global navigation be based on a model Division (i.e. the CharField in the model will be used as the button text in the global nav, and the id will be used to build the URL). I thought tags might work, but what I end up with is this (yes, I'm new to Django and Python): current_tags.py from django import template # Import your model from libs

How do I get a files absolute path after being uploaded in Django?

一笑奈何 提交于 2019-12-12 19:11:56
问题 I want to upload a file to my database and after it is uploaded import it and eventually export the data into my database. I have the uploading working just fine but I'm not sure how to get the absolute path of the file after it is uploaded. I'm able to print out the name of the document, but if the same document name is uploaded it is appended but still shows the original file name if I call form.cleaned_data['document'].name . What can I do to get the absolute file path and then call a

Right way to escape JSON data from django template

耗尽温柔 提交于 2019-12-12 18:33:30
问题 I want to pass a dictionary from django view to a javascript file. The dictionary is built from a database populated by site users. What's the difference between these 2 methods in terms of security? var mydata = JSON.parse("{{mydata|escapejs}}"); var mydata = {{ mydata|safe }}; Further, the doc at django says this for escapejs : This does not make the string safe for use in HTML . Could you show me an example of how it's unsafe & how can I make it safe. 回答1: The following dictionary can

Adding first name and last name to Django-Registration

痞子三分冷 提交于 2019-12-12 15:36:47
问题 I have used the default Django registration (version 0.8) in my project where users only input their username, email, and password. However, I want users to also input their first name and last name on the registration page. How can I do this easily? 来源: https://stackoverflow.com/questions/9023493/adding-first-name-and-last-name-to-django-registration

405 error on django ajax POST

三世轮回 提交于 2019-12-12 15:05:07
问题 I have a model with a integer field wich will increment on user click, like a "vote this" button. The button only shows on the detail view. To increment the vote count it sends an ajax POST. The problem is that django returns a 405 (method not allowed) error even before executing the view. What can be causing this? Here is my code: views.py (doesn't get executed) @require_POST def vote_proposal(request, space_name): """ Increment support votes for the proposal in 1. """ prop = get_object_or