django

How to properly serve my Angular application static files from a Django project on Heroku?

99封情书 提交于 2021-02-19 03:08:47
问题 I am trying to deploy on Heroku my app built with Django 2, DRF and Angular 6, and struggling to get the static files of my Angular app served. I am using WhitheNoise for both development environment and on Heroku. The structure of the project is as follows: my_project/ |-frontend/ | |- dist/ | |- ... |-myproject/ | |-settings/ | | |- __init.py | | |- base.py | | |- prod.py | | |- dev.py | |- __init__.py | |- urls.py | |- views.py | |- wsgi.py |-other_app1/ | |- ... |-other_app2/ | |- ... |

Pass context from one serializer to another?

谁说我不能喝 提交于 2021-02-19 02:35:09
问题 With Django REST Framework, I have 2 serializers: PageSerializer and CommentSerializer . CommentSerializer depends on some extra "context" value, but it doesn't get it directly, instead it needs to get it from PageSerializer , since they have a nested relationship. So I need to have something like this: class CommentSerializer(serializers.ModelSerializer): ... my_field = serializers.SerializerMethodField() def get_my_field(self, comment): my_value = self.context['my_value'] ... class

Django Query to Aggregate the Sum for Matching Instances in the Same Field

流过昼夜 提交于 2021-02-19 02:34:35
问题 I have the following Django (2.1) model: class Sales(models.Model): product_name = models.ForeignKey(Product) category = models.ForeignKey(Category) sales= models.DecimalField(max_digits=25) I would like to receive the sum of all sales for each different product_name instance that is the same. For example, I have the following sales by product_name : DVDs = 6.00 DVDs = 6.00 Books = 14.00 Books = 6.00 Magazines = 4.00 Magazines = 4.00 I can use Sales.objects.aggregate(sales=Sum('sales')) to

How to retrieve image files from mongodb to html page

我的未来我决定 提交于 2021-02-19 02:27:07
问题 I have successfully stored image files in mongdb in binary format.but when i m getting image from mongodb i m getting the same banary format.But i need this image file.Please someone could help This is the code i used def retrieve(request): db=pymongo.connection.Connection('localhost',27017).demo1 grid=gridfs.GridFS(db) output=grid.get_last_version(filename='shiva.jpg') return HttpResponse(output) 回答1: Hi i have successfully inserted and retrieved image from mongodb with python.. def insert

django:redis:CommandError: You have not set ASGI_APPLICATION, which is needed to run the server

末鹿安然 提交于 2021-02-19 02:20:10
问题 I am trying to create socket in django. I installed asgi_redis as per this link. When I ran the command python manage.py runserver, I am getting below error. >python manage.py runserver CommandError: You have not set ASGI_APPLICATION, which is needed to run the server. As I havent started the redis, above error might be because of this. I am bit confused , do I need to install Redis separately or just need to start the redis as I have already installed the asgi_redis? project/settings.py file

Show the dictionary key in django template

江枫思渺然 提交于 2021-02-19 02:06:36
问题 Im wondering how i could show the dictionary key itself in a django template Example dictionary: resources = {'coin': coin, 'grain': grain, 'iron': iron, 'stone': stone, 'wood': wood,} Template <b>Coin: </b>{{ upgrade.coin }} Were i want to use the dictionary key (+some html) instead of the hard coded "Coin:" Can anyone please help me out? 回答1: Use for tag with dict.items if you want to print all key/value pairs: {% for key, value in resources.items %} <b>{{ key }}: </b>{{ value }} {% endfor

Django: Why is Foo.objects.extra(…) So Much Faster Than Foo.objects.raw?

匆匆过客 提交于 2021-02-19 01:59:28
问题 So I am trying to optimize a fairly odd query, but this is a legacy database so I make do with what I have. These are the queries I am trying. They provide the same output at this point. w is my queryset. def future_schedule(request): past = datetime.date.today()-datetime.timedelta(days=730) extra_select = { 'addlcomplete': 'SELECT Complete FROM tblAdditionalDates WHERE Checkin.ShortSampleID = tblAdditionalDates.ShortSampleID', 'addldate': 'SELECT AddlDate FROM tblAdditionalDates WHERE

python主流框架简介和Django框架的使用

倖福魔咒の 提交于 2021-02-19 01:51:53
[TOC] 一、手撸简易web框架 通过统一思想,对各个功能或者接口进行统一,越来越像是Django框架的流程,实现了低配版的Django。下面是具体的流程图 具体步骤为:wsgiref库拿到用户的请求内容,我们判断用户请求的链接地址内容,匹配到对应的后端函数,后端函数内部调用数据库得到需要的数据,再拿到html文件进行读取后,之后再把数据通过jinja2模块语法插入html文件中,最后再把修改过后的html文件return出去。wsgiref库把该html文件内容发送回浏览器,浏览器拿到内容后展示在页面上。结束。 基于第三方模块帮你撸 使用了wsgiref库和jinja2库 wsgiref库帮我们封装了http协议,并将http的请求头和请求首行变成字典。即将请求内容解包,响应的内容压包。 jinja2库专门用来处理后端数据与html页面的交互 views.py 里面的放的是函数 我们管这些函数叫视图函数 视图层 urls.py 里面放的是 路由(后缀)与视图函数的对应关系 路由层 templates文件夹 里面放的全是html文件 模板层 二、动静态网页 1. 静态网页 数据是写死的 万年不变(哪怕改变了 也是人为直接修改) 2. 动态网页 数据是实时获取的 eg: 1 后端代码动态获取 当前时间 2 数据是从数据库查询出来的 三、jinja2模板语法 1. jinja2的作用

Django nested URLs

雨燕双飞 提交于 2021-02-19 01:05:23
问题 How do I nest url calls in django? For example, if I have two models defined as class Post(models.Model): title = models.CharField(max_length=50) body = models.TextField() created = models.DateTimeField(auto_now_add=True, editable=False) def __unicode__(self): return self.title @property def comments(self): return self.comment_set.all() class Comment(models.Model): comment = models.TextField() post = models.ForeignKey(Post) created = models.DateTimeField(auto_now_add=True) With the following

Django nested URLs

只愿长相守 提交于 2021-02-19 01:04:15
问题 How do I nest url calls in django? For example, if I have two models defined as class Post(models.Model): title = models.CharField(max_length=50) body = models.TextField() created = models.DateTimeField(auto_now_add=True, editable=False) def __unicode__(self): return self.title @property def comments(self): return self.comment_set.all() class Comment(models.Model): comment = models.TextField() post = models.ForeignKey(Post) created = models.DateTimeField(auto_now_add=True) With the following