做我的第一个真正的Django项目,需要指导.
背景: 我的项目是一个reddit克隆.用户提交链接+文本.访客upvote或downvote.有一个社交驱动算法,每隔约2分钟作为背景脚本运行,根据净投票和内容的新鲜度重新提交所有提交.相当香草的东西.
问题:
排序votes不正常,因为votes正在初始化None而不是0.这导致None投票的提交排名低于负投票的提交.我已经调试了这个问题好几天 - 没有运气.
细节:
我已经过度使用我的模型的模型管理器来Sum为查询集注释聚合函数,然后通过"社会等级"和投票来对所述查询集进行排序.
下面是我的models.py.我正在使用Django 1.5,因此你在这里看到的一些东西可能与1.8不相符(例如get_query_setvs get_queryset):
class LinkVoteCountManager(models.Manager):
def get_query_set(self):
return super(LinkVoteCountManager, self).get_query_set().annotate(votes=Sum('vote__value')).order_by('-rank_score', '-votes')
class Link(models.Model):
description = models.TextField(_("Write something"))
submitter = models.ForeignKey(User)
submitted_on = models.DateTimeField(auto_now_add=True)
rank_score = models.FloatField(default=0.0)
url = models.URLField(_("Link"), max_length=250, blank=True)
with_votes = LinkVoteCountManager()
objects = models.Manager()
def __unicode__(self):
return self.description
def set_rank(self):
# Based on reddit ranking algo at http://amix.dk/blog/post/19588
epoch …Run Code Online (Sandbox Code Playgroud) python django django-models django-queryset django-aggregation
我正在使用以下两种方法来计算设置字体类型和大小的示例字符串的渲染宽度:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)
sample = "Lorem ipsum dolor sit amet, partem periculis an duo, eum lorem paulo an, mazim feugiat lobortis sea ut. In est error eirmod vituperata, prima iudicabit rationibus mel et. Paulo accumsan ad sit, et modus assueverit eum. Quod homero adversarium vel ne, mel noster dolorum te, qui ea senserit argumentum complectitur. Duo at laudem explicari deterruisset, eu quo hinc mnesarchum. Vel autem insolens atomorum at, dolorum suavitate voluptatum duo ex."
#METHOD 1 …Run Code Online (Sandbox Code Playgroud) 尝试转到我的Django应用程序的管理面板时出现此错误:
在/admin/index.html上的TemplateDoesNotExist
例外位置:/home/mhb11/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/template/loader.py in find_template,139行
模板加载器postmortem Django尝试按以下顺序加载这些模板:使用loader django.template.loaders.filesystem.Loader:使用loader django.template.loaders.app_directories.Loader:/ home/mhb11/folder/project/templates/admin /index.html(文件不存在)/home/mhb11/.virtualenvs/projectenv/local/lib/python2.7/site-packages/bootstrap_pagination/templates/admin/index.html(文件不存在)
我有一个新的Django安装,没有移动任何文件,并且django.contrib.admin在INSTALLED_APPS中添加了settings.py .
如果我去,/.virtualenvs/projectenv/local/lib/python2.7/site-packages/django/contrib/admin/我没有找到一个/templates/文件夹(其中包含index.html和一堆其他文件).
但是,安装在不同机器上的完全相同的项目是否具有该/templates/文件夹.我在我的虚拟环境中卸载并重新安装了Django,但无济于事.
在settings.py中安装的应用程序具有以下内容:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
#'django.contrib.sessions',
'user_sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.comments',
'myapp',
'myproject',
'south',
'registration',
'bootstrap_pagination',
'djcelery',
'tweepy',
'django.contrib.humanize',
'analytical',
'mathfilters',
#'request',
#'debug_toolbar',
#'analytical',
#'django_whoshere',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
我得到的错误的完整痕迹是:
Internal Server Error: /admin/ Traceback (most recent call last): File "/home/mhb11/.virtualenvs/projectenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", …Run Code Online (Sandbox Code Playgroud) 在 Django 项目中,我使用了 Django 的内置模板系统。每天都有不错的流量,我一直专注于减少服务器响应时间,以改善用户体验并减少服务器负载。
然而,我没有做太多来改善页面加载时间。这是当前的快照:
不用说,优化 DOM 处理(黄色)和页面渲染(蓝色)还有很多空间需要覆盖。在我正在考虑的各种优化中,一个是用 Jinja2 替换 Django 的内置模板系统。
但这会有帮助吗?
在我自己的研究中,我读到 Jinja2 模板的渲染速度比 Django 的内置模板快得多。
对此有些陌生,我需要知道在 Django 的库存模板系统与 jinja2 之间对模板渲染和 DOM 处理进行基准测试的最佳方法。例如,我有 django 调试工具栏 - 这有帮助吗?我可以使用哪些其他工具?能否提供一个简单的说明性示例?谢谢!
有什么方法可以以编程方式折叠当Google广告不展示时产生的空白吗?如果是这样,我希望看到一个说明性的例子。
到处搜索使我进入了这个Google官方资源,可以准确地完成我的要求。但是,这与DFP广告管理系统有关,很遗憾,DFP广告管理系统是单独的产品。我想知道如何针对AdSense处理此问题-目前,我的一些用户正在盯着空白处。
如果需要解决的话,下面是Google AdSense提供的一个示例广告片段(我将其居中对齐):
<div style="text-align:center">
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-0000000000000000"
data-ad-slot="0044031319"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
Run Code Online (Sandbox Code Playgroud) 我有一个 Django Web 应用程序托管在一个虚拟机上,该虚拟机以基于 Debian 的 Ubuntu 作为操作系统,以 nginx 反向代理 + Gunicorn 作为 Web 服务器。
此 Web 应用程序的 DNS 是myapp.cloudapp.net。我还有一个 ccTLD,mydomain.pk需要配置为该 Web 应用程序的自定义域名。
我原来的注册商只支持名称服务器。因此,我在 dns.he.net(一个免费的 DNS 托管提供商)上创建了一个帐户来托管我的名称服务器,并为我的计算机设置 CName。
我的问题是,一旦我将 CName 设置为指向我的 Web 应用程序的 DNS,在浏览器中输入 mydomain.pk 只会显示一个通用Welcome to ngnix!页面。然而,在浏览器中输入myapp.cloudapp.net(或myapp.cloudapp.net:80) 可以正确打开 Web 应用程序。为什么设置 CName 不起作用?
我已经与 dns.he.net 的支持人员交谈过 - 我被告知我的 CName 设置正确,并且我的 nginx 配置可能存在一些问题。
例如,这是我的etc/nginx/sites-available/myproject文件:
server {
listen 80;
server_name myapp.cloudapp.net;
charset utf-8;
underscores_in_headers on;
location = /favicon.ico { access_log …Run Code Online (Sandbox Code Playgroud) 在我的Django项目中,我正在使用DateTimeField模型.这基本上有python datetime.datetime实例.
从纪元(以秒为单位)转换到时间的最快方法是什么?
有没有人有任何关于直接使用azang-storage与Django的提示?我问,因为目前我正在尝试为我的Django应用程序设置Azure云存储(使用Ubuntu OS托管在Azure VM上),并且django-storages似乎没有正确地与Azure Storage SDK连接(已知问题:请参阅此处).鉴于我的Django版本<1.6.2,那里列出的修复程序对我不起作用.
因此,我需要直接使用Azure存储与Django.有人有过这样的话吗?我需要在云存储上保存图像和mp3.
目前,在我的models.py中,我有:
def upload_to_location(instance, filename):
try:
blocks = filename.split('.')
ext = blocks[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
instance.title = blocks[0]
return os.path.join('uploads/', filename)
except Exception as e:
print '%s (%s)' % (e.message, type(e))
return 0
class Photo(models.Model):
description = models.TextField(validators=[MaxLengthValidator(500)])
submitted_on = models.DateTimeField(auto_now_add=True)
image_file = models.ImageField(upload_to=upload_to_location, null=True, blank=True )
Run Code Online (Sandbox Code Playgroud)
然后django-storages,boto照顾其余的.但是,当我使用Azure云存储挂起django-storage时,我收到以下错误:
Exception Value:
'module' object has no attribute 'WindowsAzureMissingResourceError'
Exception Location: …Run Code Online (Sandbox Code Playgroud) 在 下supervisor,celerybeat 为我的 Django 应用程序的 celery 工作人员提供定期任务。我有 4 个任务,task1、task2、task3和task4。最近我做了第五个任务:task5。
我的问题是我从我的工作人员中注释掉了它task5,从 settings.py 中删除了它的提及并重新启动了 celerybeat 和我的 celery 工作人员。但我仍然看到task5定期出现(自然地在工人日志中抛出错误)。
为什么会发生这种情况,如何更新定期任务?
在 settings.py 中,我有:
import djcelery
djcelery.setup_loader()
# config settings for Celery Daemon
# Redis broker
BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
# List of modules to import when celery starts, in myapp.tasks form.
CELERY_IMPORTS = ('myapp.tasks', )
CELERY_ALWAYS_EAGER = False
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
#The backend is the …Run Code Online (Sandbox Code Playgroud) django ×6
python ×5
adsense ×1
adsense-api ×1
azure ×1
celery ×1
celery-task ×1
celerybeat ×1
cname ×1
django-admin ×1
dns ×1
jinja2 ×1
nginx ×1
pillow ×1
redis ×1
supervisord ×1
templates ×1
webserver ×1