django-views

Redirect anonymous users to log in (don't show them anything)

点点圈 提交于 2020-03-28 05:10:59
问题 Django 1.9.6. I want to absolutely disable the whole website from viewing by anonymous users. Anonymous users will always be redirected to login page. I have created a general view. The problem is that subclasses of GeneralView may not just render a template but perform some calculations or just be of different kinds: DetailView, ListView etc. class GeneralView(View): def get(self, request, template): if not request.user.is_authenticated() and request.user.is_active: return redirect("auth

Redirect anonymous users to log in (don't show them anything)

房东的猫 提交于 2020-03-28 05:09:05
问题 Django 1.9.6. I want to absolutely disable the whole website from viewing by anonymous users. Anonymous users will always be redirected to login page. I have created a general view. The problem is that subclasses of GeneralView may not just render a template but perform some calculations or just be of different kinds: DetailView, ListView etc. class GeneralView(View): def get(self, request, template): if not request.user.is_authenticated() and request.user.is_active: return redirect("auth

Parsing static json file and save values to Django Database/model

六月ゝ 毕业季﹏ 提交于 2020-03-26 03:55:06
问题 I have json file in my static folder in my django project. And I want to save the data from json file to my Django Database. This is my model.py from django.db import models class Coming_Soon(models.Model): movie_id = models.CharField(primary_key=True, max_length=11) movie_title = models.TextField(blank=True, max_length=100) image_url = models.TextField(blank=True) synopsis = models.TextField(blank=True) rating = models.TextField(default="MTRCB rating not yet available") cast = models

Django cannot create or render comments

ε祈祈猫儿з 提交于 2020-03-24 00:16:46
问题 Hi have this structure to handle users posts and comments my post model is from django.db import models from django.contrib.auth.models import User class Post(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, default=0) profile = models.ForeignKey('users.Profiles', on_delete=models.CASCADE, null=True, default=1, blank=True) title = models.CharField(max_length=100, blank=True, null=True) desc = models.CharField(max_length=1000, blank=True, null=True) photo = models

Django 2.0 url parameters in get_queryset

瘦欲@ 提交于 2020-03-19 05:11:36
问题 I would like to filter subcategories based on the category id from the url For a constant value it works without a problem return Subcategory.objects.filter(category = 1) views.py class SubcategoriesListView(ListView): model = Subcategory template_name = 'app/categories/index.html' def get_queryset(self): return Subcategory.objects.filter(category = category_id) urls.py path('categories/<int:category_id>/', app.views.SubcategoriesListView.as_view(), name='subcategories'), models.py class

How to get parameters from current url

我的梦境 提交于 2020-03-02 09:39:08
问题 Is it possible to get an specific parameter in a url and use it in a template ? `{{ request.path }} It gets the whole url, i need only the 'pk' parameter, to make a link to another page. Thanks. 回答1: I did some experiments and find that this may help: {{ request.resolver_match.kwargs.pk }} For more info you can check this. 来源: https://stackoverflow.com/questions/40797746/how-to-get-parameters-from-current-url

Django rest framework where to write complex logic in serializer.py or views.py?

风格不统一 提交于 2020-02-28 07:48:30
问题 I am new to Django Rest Framework. Using serializer and views a simple CRUD is easy. When the logics increase, it is quite confusing where to write logics in serializer or views . Some developers do prefer "Thick serializer and thin views" and some developers "Thick views and thin serializer". Maybe this is not a big issue and I think it is up to the developer whether to write more on views or serializer , but as a newbie what will be your suggestion to follow? Should I write more on views or

Django ImportError: cannot import name 'render_to_response' from 'django.shortcuts'

北城余情 提交于 2020-02-27 23:11:03
问题 After upgrading to Django 3.0, I get the following error: ImportError: cannot import name 'render_to_response' from 'django.shortcuts' My view: from django.shortcuts import render_to_response from django.template import RequestContext def index(request): context = {'foo': 'bar'} return render_to_response('index.html', context, context_instance=RequestContext(request)) Here is the full traceback: Traceback (most recent call last): File "./manage.py", line 21, in <module> main() File "./manage

How to provide user constant notification about Celery's Task execution status?

爷,独闯天下 提交于 2020-02-25 13:26:26
问题 I integrated my project with celery in this way, inside views.py after receving request from the user def upload(request): if "POST" == request.method: # save the file task_parse.delay() # continue and in tasks.py from __future__ import absolute_import from celery import shared_task from uploadapp.main import aunit @shared_task def task_parse(): aunit() return True In short, the shared task will run a function aunit() from a third python file located in uploadapp/ directory named main.py .

How to provide user constant notification about Celery's Task execution status?

余生长醉 提交于 2020-02-25 13:24:51
问题 I integrated my project with celery in this way, inside views.py after receving request from the user def upload(request): if "POST" == request.method: # save the file task_parse.delay() # continue and in tasks.py from __future__ import absolute_import from celery import shared_task from uploadapp.main import aunit @shared_task def task_parse(): aunit() return True In short, the shared task will run a function aunit() from a third python file located in uploadapp/ directory named main.py .