django-views

Updateview with dynamic form_class

你离开我真会死。 提交于 2021-02-07 20:40:51
问题 I would like to dynamically change the form_class of an UpdateView CBV in Django 1.6. I've tried to do this using the get_context_data(), but that didn't help since the form is already initialized. So it will need to happen during __init__ , I guess. Here's what I've tried on __init__ : class UpdatePersonView(generic.UpdateView): model = Person form_class = "" def __init__(self, *args, **kwargs): super(UpdatePersonView, self).__init__(*args, **kwargs) person = Person.objects.get(id=self.get

Updateview with dynamic form_class

醉酒当歌 提交于 2021-02-07 20:38:16
问题 I would like to dynamically change the form_class of an UpdateView CBV in Django 1.6. I've tried to do this using the get_context_data(), but that didn't help since the form is already initialized. So it will need to happen during __init__ , I guess. Here's what I've tried on __init__ : class UpdatePersonView(generic.UpdateView): model = Person form_class = "" def __init__(self, *args, **kwargs): super(UpdatePersonView, self).__init__(*args, **kwargs) person = Person.objects.get(id=self.get

Save Base64 String into Django ImageField

ⅰ亾dé卋堺 提交于 2021-02-07 12:19:02
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

Add context to every Django Admin page

℡╲_俬逩灬. 提交于 2021-02-07 12:18:40
问题 How do I add extra context to all admin webpages? I use default Django Admin for my admin part of a site. Here is an url entry for admin: urlpatterns = [ url(r'^admin/', admin.site.urls), ] And my apps register their standard view models using: admin.site.register(Tag, TagAdmin) My problem, is that I want to display an extra field in admin template header bar and I have no idea how to add this extra context. My first bet was adding it in url patterns like below: urlpatterns = [ url(r'^admin/'

Add context to every Django Admin page

微笑、不失礼 提交于 2021-02-07 12:16:30
问题 How do I add extra context to all admin webpages? I use default Django Admin for my admin part of a site. Here is an url entry for admin: urlpatterns = [ url(r'^admin/', admin.site.urls), ] And my apps register their standard view models using: admin.site.register(Tag, TagAdmin) My problem, is that I want to display an extra field in admin template header bar and I have no idea how to add this extra context. My first bet was adding it in url patterns like below: urlpatterns = [ url(r'^admin/'

Save Base64 String into Django ImageField

空扰寡人 提交于 2021-02-07 12:15:32
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

Save Base64 String into Django ImageField

巧了我就是萌 提交于 2021-02-07 12:15:30
问题 Im doing an application that uses Django in server-side. Im trying to do that: import uuid from base64 import b64decode from django.core.files.base import ContentFile @staticmethod def add_photo(user, person, image_base64): photo = DatabasePersonPhoto() photo.user = user photo.person = person image_data = b64decode(image_base64) image_name = str(uuid.uuid4())+".jpg" photo.image = ContentFile(image_data, image_name) photo.save() return photo This is my Base64 String: data:image/jpeg;base64,/9j

How should I show results of BeautifulSoup parsing in Django?

倖福魔咒の 提交于 2021-02-07 10:17:29
问题 I'm trying to scrape a web page using BeautifulSoup and Django. Here's my views.py which do this task: def detail(request, article_id): article = get_object_or_404(Article, pk=article_id) html = urllib2.urlopen("...url...") soup = BeautifulSoup(html) title = soup.title return render(request, 'detail.html', {'article': article, 'title':title}) But when I use {{ title }} in django template files, it doesn't show anything. I've test it and it works in shell. I've added a line to this function:

How should I show results of BeautifulSoup parsing in Django?

一曲冷凌霜 提交于 2021-02-07 10:16:33
问题 I'm trying to scrape a web page using BeautifulSoup and Django. Here's my views.py which do this task: def detail(request, article_id): article = get_object_or_404(Article, pk=article_id) html = urllib2.urlopen("...url...") soup = BeautifulSoup(html) title = soup.title return render(request, 'detail.html', {'article': article, 'title':title}) But when I use {{ title }} in django template files, it doesn't show anything. I've test it and it works in shell. I've added a line to this function:

custom template loader Django

百般思念 提交于 2021-02-07 09:52:29
问题 i am trying to write a custom template loader in django which serves index.html which is present in a s3 bucket. Following is my loader file from django.conf import settings from django.template import Origin, Engine from django.template.loader import TemplateDoesNotExist from django.template.loaders.base import Loader from boto3.session import Session ACCESS_KEY_NAME = getattr(settings, 'AWS_TEMPLATE_LOADING_ACCESS_KEY_ID', getattr(settings, 'AWS_TEMPLATE_LOADING_ACCESS_KEY_ID', None))