django-2.2

Overwrite save method with conversion from float to string

左心房为你撑大大i 提交于 2019-12-25 01:24:23
问题 I'm using django-mapbox-location-field and I need to save automatically the data from LocationField() into another field named coordinates . This is my model: class AddPoint(models.Model): point = LocationField() coordinates = models.CharField( max_length=50, blank=True, null=True, ) def save(self, *args, **kwargs): lat = self.point[0] lon = self.point[1] lon_lat = str(lon) + ', ' + str(lat) self.coordinates = lon_lat super(AddPoint, self).save(*args, **kwargs) Everytime I try to add a point

Django 2.2 Not Serving Static Files

不羁的心 提交于 2019-12-24 11:15:42
问题 I am currently working through some tutorials to enhance my knowledge of Django. In doing so, I decided to use to most recent version of Django. I have been able to overcome most of the divergences between my code and that of the tutorial except for one - static files are not being served. Now, I am not fully sure that the difference is strictly due to the Django version. Here is what is going on. I have the following project structure: settings.py STATIC_URL = '/static/' STATIC_FILES = ( os

How can display the label of non-editable fields in django template

*爱你&永不变心* 提交于 2019-12-13 02:54:18
问题 I have this field in my model: created_date = models.DateTimeField(null=True, blank=True,editable=False, verbose_name=_("Something to show... ")) In accordance with this document ,I show the value of this field by: form.instance.created_date I see these links: 1,2,3,4 and this docucment of django. But none of them talk about showing the label (Or verbos_name ) of non-editable fields in template. Also I test these possible modes: 1. form.instance.created_date.label_tag 2. form.instance.created

Double filter on detail view

余生颓废 提交于 2019-12-12 06:51:29
问题 I'm trying to create a detail view using function based view. This view must make visible only the already published posts and the non draft posts. def singlePost(request, slug_post, slug_category): post_category = get_object_or_404(BlogCategory, slug_category=slug_category) if post_filter == BlogPost.objects.filter(draft=True): raise PermissionDenied if post_filter == BlogPost.objects.filter(publishing_date__gt=datetime.datetime.now()): raise PermissionDenied else: post_filter == BlogPost

Django how to iterate querysets of two subclasses in template?

此生再无相见时 提交于 2019-12-11 18:29:43
问题 I am using django-model-utils for inheritance Managers. I want to get result of both sub-classes with one dictionary without getting duplicates. models.py class Images(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='images_created', on_delete=models.CASCADE) created = models.DateTimeField(auto_now_add=True, null=True, blank=True) objects = InheritanceManager() class Postimg(Images): user_img= models.ImageField(upload_to='images/%Y/%m/%d', null=True, blank=True)

How to render a CheckboxSelectMultiple form using forms.ModeForm that uses data from DB as :-> SomeModel.objects.filter(user=request.user)

对着背影说爱祢 提交于 2019-12-11 16:46:44
问题 Please take a look at the code and if you can not comprehend what is going on, I am explaining it in the end I have a model named Good class Good(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) ans = models.CharField(max_length=1024) and a form class GoodForm(forms.ModelForm): def __init__(self, request=None, *args, **kwargs): super(GoodForm, self).__init__(*args, **kwargs) self.input_list = request.user.love_set.all() self.fields['ans'] = forms.MultipleChoiceField(

Save list of records and fields into django model

╄→гoц情女王★ 提交于 2019-12-11 09:56:13
问题 I receive this list from my website admin: [['present', '2'],['present', '1'], ['study', '1'], ['study', '3'], ['present', '4'], ['study', '4'], The first option is actually the field name that needs to be edit in Rollcall model and the second option is the user ID. Now I want to save this list to the Rollcall model: #models.py class Rollcall(models.Model): student = models.ForeignKey(User) present = models.BooleanField(default=False) study = models.BooleanField(default=False) So I first

Create a restricted area

空扰寡人 提交于 2019-12-11 04:29:06
问题 I'm trying to build a restricted area for a website in which only the registered user can see his personal profile and his posts. The staff users can see all users profile and related posts. This is models.py : ... from django.contrib.auth.models import User class Post(models.Model): authorized_users = models.ManyToManyField( User, related_name="user_set", default=1, ) title = models.CharField(max_length=100) ... As you can see one post can have more then one author( authorized_users ). This

“No installed app with label 'admin'” in empty Django 2.2 project

一曲冷凌霜 提交于 2019-12-04 08:52:14
I am starting a new project in Django 2.2 using the same commands as I always do: python3 -m venv django-env . django-env/bin/activate pip install django django-admin startproject mysite cd mysite/ python3 manage.py runserver 8080 This should result in a running Django site, ready to write some code. Unfortunately, now I'm getting a strange error: LookupError: No installed app with label 'admin'. Full traceback: Watching for file changes with StatReloader Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner

Django Nested Views

假装没事ソ 提交于 2019-12-02 16:13:20
问题 I'm developing an internal application and I would like to be able to nest my views to keep everything nice and organized. I plan on doing this by keeping different parts of the page in their own HTML files with their own Views (separate sidebar and navbar, separate charts, etc). views.py from django.shortcuts import render from django.views.generic import TemplateView import Recall.data_logger.models as DLM class ReportHome(TemplateView): template_name = 'data_logger/index.html' class