我正在为Django应用程序添加一组模板标签,我不知道如何测试它们.我已经在我的模板中使用它们,它们似乎正在工作但我正在寻找更正式的东西.主要逻辑在模型/模型管理器中完成,并已经过测试.标签只是检索数据并将其存储在上下文变量中,例如
{% views_for_object widget as views %}
"""
Retrieves the number of views and stores them in a context variable.
"""
# or
{% most_viewed_for_model main.model_name as viewed_models %}
"""
Retrieves the ViewTrackers for the most viewed instances of the given model.
"""
Run Code Online (Sandbox Code Playgroud)
所以我的问题是你通常测试你的模板标签,如果你这样做你怎么做?
我想通过相关字段对联系人的QuerySet进行排序.但是我不知道怎么做.我试过这样,但它不起作用.
foundContacts.order_by("classification.kam")
Run Code Online (Sandbox Code Playgroud)
实际上在模板中我可以通过contact.classification.kam访问联系人的kam值,因为它是OneToOne关系.
(简化)模型如下所示:
class Classification(models.Model):
kam = models.ForeignKey(User)
contact = models.OneToOneField(Contact)
class Contact(models.Model):
title = models.ForeignKey(Title, blank=True, null=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将django与sqlserver连接.我已经安装了python odbc和django-odbc.
我的数据数据库配置(settings.py)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'EDAS', # Or path to database file if using sqlite3.
'USER': 'sa', # Not used with sqlite3.
'PASSWORD': '1324', # Not used with sqlite3.
'HOST': 'DBIO01-HP', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '1433' # Set to empty string for default. Not used with sqlite3.
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行服务器时,我收到此错误:
C:\ edas> python manage.py runserver
Validating models...
Unhandled exception …
Run Code Online (Sandbox Code Playgroud)