django-views

Class Based Views VS Function Based Views

大城市里の小女人 提交于 2019-11-28 15:47:27
I always use FBVs (Function Based Views) when creating a django app because it's very easy to handle. But most developers said that it's better to use CBVs (Class Based Views) and use only FBVs if it is complicated views that would be a pain to implement with CBVs. Why? What are the advantages of using CBVs? The single most significant advantage is inheritance. On a large project it's likely that you will have lots of similar views. Rather than write the same code again and again, you can simply have your views inherit from a base view. Also django ships with a collection of generic view

django - update model with FormView and ModelForm

此生再无相见时 提交于 2019-11-28 15:22:32
问题 I can't figure out how to use a ModelForm in a FormView so that it updates an already existing instance?? The form POSTs on this URL: r'/object/(?P<pk>)/' I use a ModelForm (and not directly an UpdateView ) because one of the fields is required and I perform a clean on it. I'd basically like to provide the kwarg instance=... when initializing the form in the FormView (at POST) so that it's bound to the object whose pk is given in the url. But I can't figure out where to do that... class

How to bookmark a page in Django?

耗尽温柔 提交于 2019-11-28 14:51:16
I am trying to edit an html page so a logged in user can favorite / bookmark a video.id Here is the .html file <td> <form method='POST' action="{% url 'researcher_view_app:favourite_post' video.id %}"> {% csrf_token %} <input type='hidden' name='video' value={{ video.id }}> <button type='submit'>Bookmark</button> </form> </td> Here is the urls.py file path('<int:fav_id>/favourite_post', views.favourite_post, name='favourite_post'), Here is the view.py file def favourite_post(request, fav_id): video = get_object_or_404(Video, id=fav_id) if request.method == 'POST': video. return render(request,

Any /polls URL always call index() function in views.py

一个人想着一个人 提交于 2019-11-28 14:44:43
polls/urls/py from django.conf.urls import url from . import views urlpatterns = [ url('', views.index, name='index'), url('<int:question_id>/', views.detail, name='detail'), # ex: /polls/5/results/ url('<int:question_id>/results/', views.results, name='results'), # ex: /polls/5/vote/ url('<int:question_id>/vote/', views.vote, name='vote'), ] views.py from __future__ import unicode_literals from django.http import HttpResponse from .models import Question from django.template import loader # from django.shortcuts import render def index(request): latest_question_list = Question.objects.order

Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\\\-(?P<news_pk>[0-9]+)$']

本小妞迷上赌 提交于 2019-11-28 14:38:08
This issue only happens in production environment ,in my local development environment it works well. Template error: In template /home/william/kjmg/templates/base.html, error at line 0 Reverse for 'news_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['news\\-(?P<news_pk>[0-9]+)$'] 1 : <!doctype html> 2 : <html lang="en"> 3 : {% load staticfiles %} 4 : <head> 5 : <!-- Required meta tags --> 6 : <meta charset="utf-8"> 7 : <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 8 : {# 导航栏图标显示 <link rel="icon" href="{% static 'images/logo-w.png' %}">#

Django media files doesn't work when debug=false

寵の児 提交于 2019-11-28 14:37:17
settings.py STATIC_URL = '/static/' STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage' STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')] STATIC_ROOT = os.path.join(BASE_DIR, 'static') MEDIA_ROOT = os.path.join(BASE_DIR, 'data') MEDIA_URL = '/data/' urls.py urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) handler404 = 'generic.views.invalid_request' When I set DEBUG=False and run the server using python manage.py runserver --insecure all static file are serve successfully but media files doesn't appear. In debug console media urls

How to pass extra_context when redirecting in Django

人盡茶涼 提交于 2019-11-28 13:14:33
My views.py: @login_required def some_views(request): if request.method == 'POST': form = AddressCreateFrom(request.POST) if form.is_valid(): name = form.cleaned_data['Address'] ip_value = form.cleaned_data['value'] user_list = get_username(name) address_create = form.save() extra_context = { 'user_list': user_list } return redirect_to(request, url=address_create.get_absolute_url()) else: form = AddressCreateFrom() extra_context = { 'form':AddressCreateFrom(initial={'user': request.user.pk}) } return direct_to_template(request,'networks/user_form.html',extra_context) In form.py: class

render_to_response or redirect changes the template elements in Django 1.8

别说谁变了你拦得住时间么 提交于 2019-11-28 13:00:05
I'm trying to check if email id entered by user is existing in the database table, if existing - I would like to route to 'prof.html' template otherwise just show a message in the login.html template. Both the conditions are working fine. However, the problem is when I use redirect() or render_to_response() - the destination template elements like div, input etc., are being changed automatically (prof.html in this case) ? Can we also send the context information to destination template ? (response data or any object from the database and redirect to prof.html template via view in this case)

Attendance system using Django

天涯浪子 提交于 2019-11-28 12:43:25
问题 Models attendance_choices = ( ('absent', 'Absent'), ('present', 'Present') ) class Head_of_department(models.Model): first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) email = models.CharField(max_length=30) def __str__(self): return self.first_name class Employee(models.Model): first_name = models.CharField(max_length=200, unique=True) last_name = models.CharField(max_length=200, unique=True) head_of_department = models.ForeignKey('Head_of_department',

How to avoid repeating same code block in Django?

戏子无情 提交于 2019-11-28 12:31:53
问题 I have the same code block in 4 of my functions,is any way that can avoid repeating the same code block? Here is the same code block: def function_name(): ...some code... hot_news_48h = h_mostViewed(48, News, '-pv') hot_news_1w = w_mostViewed(1, News, '-pv') ...some code... return render(request, "template_name.html", { ...some code... 'hot_news_48h': hot_news_48h, 'hot_news_1w': hot_news_1w, ...some code... }) Here is function1: def newsDetailView(request, news_pk): news = get_object_or_404