django-views

How do I call a Django function on button click?

天大地大妈咪最大 提交于 2019-11-28 04:42:55
I am trying to write a Django application and I am stuck at how I can call a view function when a button is clicked. In my template, I have a link button as below, when clicked it takes you to a different webpage: <a target="_blank" href="{{ column_3_item.link_for_item }}">Check It Out</a> When the button is clicked, I also want to call a Django view function (along with re-direct to a target website). The view function increments the value in the database which stores the number of times the button is clicked. The column_3_item.link_for_item is a link to an external website (e.g. www.google

Django - two views, one page

此生再无相见时 提交于 2019-11-28 04:32:10
Say we have a Django page that shows a list of items and allows the user to fill in a form to add to the items (let's call the items posts). What I want: The URL for this page refers to a view. The view calls two other views (called "sub-view" hereon), then each sub-view renders its section and returns the results. The main view then joins the results of the sub-views and returns that. Ideally, I would have a quick javascript check on the page - if javascript is enabled, the submit button for the form will be "Ajax'd" to the sub-view that deals with form adding, and the page will be updated

Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?

柔情痞子 提交于 2019-11-28 04:20:45
I'm using Django 1.3 and I realize it has a collectstatic command to collect static files into STATIC_ROOT . Here I have some other global files that need to be served using STATICFILES_DIR . Can I make them use the same dir ? Thanks. No. In fact, the file django/contrib/staticfiles/finders.py even checks for this and raises an ImproperlyConfigured exception when you do so: "The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting" The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into

Best way to write an image to a Django HttpResponse()

老子叫甜甜 提交于 2019-11-28 04:20:06
I need to serve images securely to validated users only (i.e. they can't be served as static files). I currently have the following Python view in my Django project, but it seems inefficient. Any ideas for a better way? def secureImage(request,imagePath): response = HttpResponse(mimetype="image/png") img = Image.open(imagePath) img.save(response,'png') return response (Image is imported from PIL.) StefanNch Well, re-encoding is needed sometimes (i.e. applying an watermark over an image while keeping the original untouched), but for the most simple of cases you can use: try: with open(valid

How do I pass variables from one view to another and render with the last view's URL in Django?

老子叫甜甜 提交于 2019-11-28 04:16:56
问题 I'm building a student management system using Django. In this code, The user search for a student with the encrypted query name=StudentName&grade=Grade&id=StudentID&phone=ParentPhoneNumber&report=StudentReportNumber , that is extracted with the decrypt() method. Here are the two methods, the one which process the query and the one which shows the student profile. No data from the query is saved to the database, but will be used to query the student details from the database. def process

Django Passing data between views

旧城冷巷雨未停 提交于 2019-11-28 03:57:14
I was wondering what is the 'best' way of passing data between views. Is it better to create invisible fields and pass it using POST or should I encode it in my URLS? Or is there a better/easier way of doing this? Sorry if this question is stupid, I'm pretty new to web programming :) Thanks There are different ways to pass data between views. Actually this is not much different that the problem of passing data between 2 different scripts & of course some concepts of inter-process communication come in as well. Some things that come to mind are - GET request - First request hits view1->send

How do I use CreateView with a ModelForm

自作多情 提交于 2019-11-28 02:38:06
I get an error in my class AuthorCreateForm when I submit my form. NameError self is not defined How do I use a CreateForm? I have created a class in my Author.py file from django.views.generic import TemplateView, ListView, CreateView from books.models import Author, Publisher, Book from books.forms import AuthorForm class AuthorCreateView(CreateView): objAuthorForm = AuthorForm(self.request.POST) if(objAuthorForm.save()): success = "Form saved!" else: error = "There was an error!" and I have a html template which submits to /Author/Create and I have the following line in my urls.py ('

How to make a Django Model form Readonly?

给你一囗甜甜゛ 提交于 2019-11-28 01:19:01
问题 I have a django form name "SampleForm". Which i use to take input from user. Now i want to use same form to show this information to user on a different page. But form is editable I want to make the form read only. Is there any way to make whole form Readonly ? 回答1: pseudo-code (not tested): class ReadOnlyFormMixin(ModelForm): def __init__(self, *args, **kwargs): super(ReadOnlyFormMixin, self).__init__(*args, **kwargs) for key in self.fields.keys(): self.fields[key].widget.attrs['readonly'] =

DjangoRestFramework - registering a user: difference between UserSerializer.save() and User.objects.create_user()?

眉间皱痕 提交于 2019-11-27 22:38:18
Suppose I want to register a user (I'm using the User model located in django.contrib.auth.models). Assume this is my serializers.py: class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('username', 'password', 'email', ) What's the difference between the following views, and which one is recommended when it comes to creating a user? View A: def createUser(request): if request.method == 'POST': serializer = UserSerializer(data=request.DATA) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return

django 'str' object is not callable

﹥>﹥吖頭↗ 提交于 2019-11-27 22:14:35
I have a problem creating an URL view in django. It gives me this error (ferrol is a Space object): TypeError at /spaces/ferrol/ 'str' object is not callable Request Method: GET Request URL: http://localhost:8000/spaces/ferrol/ Django Version: 1.2.3 Exception Type: TypeError Exception Value: 'str' object is not callable Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/core/handlers/base.py in get_response, line 100 Here is the code: spaces/models.py class Space(models.Model): """ Basic spaces model. """ name = models.CharField(_('Name'), max_length=100,