标签归档:Django

Django模板中的模数%

问题:Django模板中的模数%

我正在寻找一种使用django中的模运算符之类的方法。我想做的是在循环中的每个第四个元素中添加一个类名。

使用模数,它看起来像这样:

{% for p in posts %}
    <div class="post width1 height2 column {% if forloop.counter0 % 4 == 0 %}first{% endif %}}">
        <div class="preview">

        </div>
        <div class="overlay">

        </div>
        <h2>p.title</h2>
    </div>
{% endfor %}

当然,这是行不通的,因为%是保留字符。还有其他方法吗?

I’m looking for a way to use something like the modulus operator in django. What I am trying to do is to add a classname to every fourth element in a loop.

With modulus it would look like this:

{% for p in posts %}
    <div class="post width1 height2 column {% if forloop.counter0 % 4 == 0 %}first{% endif %}}">
        <div class="preview">

        </div>
        <div class="overlay">

        </div>
        <h2>p.title</h2>
    </div>
{% endfor %}

Of course this doesn’t work because % is a reserved character. Is there any other way to do this?


回答 0

您需要divisibleby(内置的django过滤器)。

{% for p in posts %}
    <div class="post width1 height2 column {% if forloop.counter0|divisibleby:4 %}first{% endif %}">
        <div class="preview">

        </div>
        <div class="overlay">

        </div>
        <h2>p.title</h2>
    </div>
{% endfor %}

You need divisibleby, a built-in django filter.

{% for p in posts %}
    <div class="post width1 height2 column {% if forloop.counter0|divisibleby:4 %}first{% endif %}">
        <div class="preview">

        </div>
        <div class="overlay">

        </div>
        <h2>p.title</h2>
    </div>
{% endfor %}

回答 1

您不能在Django模板标签中使用模数运算符,但是编写过滤器很容易做到这一点。这样的事情应该起作用:

@register.filter
def modulo(num, val):
    return num % val

然后:

{% ifequal forloop.counter0|modulo:4 0 %}

您甚至可以执行以下操作:

@register.filter
def modulo(num, val):
    return num % val == 0

然后:

{% if forloop.counter0|modulo:4 %}

或者,您可以使用cycle标签:

<div class="post width1 height2 column {% cycle 'first' '' '' '' %}">

You can’t use the modulus operator in Django template tags, but it would be easy enough to write a filter to do so. Something like this should work:

@register.filter
def modulo(num, val):
    return num % val

And then:

{% ifequal forloop.counter0|modulo:4 0 %}

You could even do something like this, instead:

@register.filter
def modulo(num, val):
    return num % val == 0

And then:

{% if forloop.counter0|modulo:4 %}

Or you could use the cycle tag:

<div class="post width1 height2 column {% cycle 'first' '' '' '' %}">

回答 2

听起来您应该只使用cycle标签。 内置模板标签

It sounds like you should just use the cycle tag. Built-in template tags


回答 3

引导行和列的示例。每4项新行。即使少于4个项目,也请关闭最后一行。

myapp / templatetags / my_tags.py

from django import template

register = template.Library()

@register.filter
def modulo(num, val):
    return num % val

html模板

{% load my_tags %}

{% for item in all_items %} 
    {% if forloop.counter|modulo:4 == 1 %}
        <div class="row">
    {% endif %}

        <div class="col-sm-3">
            {{ item }}
        </div>

    {% if forloop.last or forloop.counter|modulo:4 == 0 %}
        </div>
    {% endif %}

{% endfor %}

Bootstrap rows and columns example. New row every 4 items. Also close last row even if there are less than 4 items.

myapp/templatetags/my_tags.py

from django import template

register = template.Library()

@register.filter
def modulo(num, val):
    return num % val

html template

{% load my_tags %}

{% for item in all_items %} 
    {% if forloop.counter|modulo:4 == 1 %}
        <div class="row">
    {% endif %}

        <div class="col-sm-3">
            {{ item }}
        </div>

    {% if forloop.last or forloop.counter|modulo:4 == 0 %}
        </div>
    {% endif %}

{% endfor %}

Mac OS X-EnvironmentError:找不到mysql_config

问题:Mac OS X-EnvironmentError:找不到mysql_config

首先,是的,我已经看到了:

pip安装mysql-python失败,并显示EnvironmentError:找不到mysql_config

问题

我正在尝试在Google App Engine项目上使用Django。但是,由于以下原因,服务器无法正常启动,因此无法启动。

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我做了一些研究,所有这些都指出必须安装Mysql-python,因为它显然不在我的系统上。我实际上尝试卸载它,并得到了:

Cannot uninstall requirement mysql-python, not installed

每当我真正尝试通过以下方式安装时:

sudo pip install MySQL-python

我收到一条错误消息:

raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

我已经尝试运行:

export PATH=$PATH:/usr/local/mysql/bin

但这似乎没有帮助,因为我再次运行了安装命令,但仍然失败。

有任何想法吗?

请注意,我不在virtualenv中。

First off, yeah, I’ve already seen this:

pip install mysql-python fails with EnvironmentError: mysql_config not found

The problem

I am trying to use Django on a Google App Engine project. However, I haven’t been able to get started as the server fails to start properly due to:

ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I did some research and it all pointed to having to install Mysql-python, as apparently it isn’t on my system. I actually tried uninstalling it and got this:

Cannot uninstall requirement mysql-python, not installed

Whenever I actually do try to install via:

sudo pip install MySQL-python

I get an error stating:

raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

I’ve already tried running:

export PATH=$PATH:/usr/local/mysql/bin

but that didn’t seem to help, as I ran the installation command again and it still failed.

Any ideas?

Please note I’m not in a virtualenv.


回答 0

好吧,首先,让我检查一下我是否和您在同一页面上:

  • 您安装了python
  • 你做了 brew install mysql
  • 你做了 export PATH=$PATH:/usr/local/mysql/bin
  • 最后,您做到了pip install MySQL-Python(或者pip3 install mysqlclient如果使用python 3)

如果您以相同的顺序执行了所有这些步骤,但是仍然出现错误,请继续阅读,如果您没有按照这些确切的步骤尝试,那么请从头开始。

因此,您按照这些步骤进行操作,仍然遇到错误,可以尝试以下操作:

  1. 尝试which mysql_config从bash 运行。可能找不到。这就是为什么构建也找不到它的原因。尝试运行locate mysql_config,看看是否有任何返回。该二进制文件的路径需要在您外壳的$ PATH环境变量中,或者必须在该模块的setup.py文件中显式地存在(假设它正在某个特定位置查找该文件)。

  2. 除了使用MySQL-Python,还可以尝试使用“ mysql-connector-python”,它可以使用安装pip install mysql-connector-python。有关此的更多信息,请参见此处此处

  3. 手动找到“ mysql / bin”,“ mysql_config”和“ MySQL-Python”的位置,并将所有这些都添加到$ PATH环境变量中。

  4. 如果以上所有步骤均失败,则可以尝试使用MacPorts安装“ mysql”,在这种情况下,文件“ mysql_config”实际上将被称为“ mysql_config5”,在这种情况下,必须在安装后执行以下操作:export PATH=$PATH:/opt/local/lib/mysql5/bin。您可以在此处找到更多详细信息。

注意1:我见过有人说安装python-dev并提供了libmysqlclient-dev帮助,但是我不知道这些软件包是否在Mac OS上可用。

注意2:此外,请确保尝试以root用户身份运行命令。

我得到了我的答案,从(除了我的大脑)这些地方(也许你可以看看他们,看看它是否会有所帮助):1234

我希望我能提供帮助,并且很高兴知道其中任何一个是否有效。祝好运。

Ok, well, first of all, let me check if I am on the same page as you:

  • You installed python
  • You did brew install mysql
  • You did export PATH=$PATH:/usr/local/mysql/bin
  • And finally, you did pip install MySQL-Python (or pip3 install mysqlclient if using python 3)

If you did all those steps in the same order, and you still got an error, read on to the end, if, however, you did not follow these exact steps try, following them from the very beginning.

So, you followed the steps, and you’re still geting an error, well, there are a few things you could try:

  1. Try running which mysql_config from bash. It probably won’t be found. That’s why the build isn’t finding it either. Try running locate mysql_config and see if anything comes back. The path to this binary needs to be either in your shell’s $PATH environment variable, or it needs to be explicitly in the setup.py file for the module assuming it’s looking in some specific place for that file.

  2. Instead of using MySQL-Python, try using ‘mysql-connector-python’, it can be installed using pip install mysql-connector-python. More information on this can be found here and here.

  3. Manually find the location of ‘mysql/bin’, ‘mysql_config’, and ‘MySQL-Python’, and add all these to the $PATH environment variable.

  4. If all above steps fail, then you could try installing ‘mysql’ using MacPorts, in which case the file ‘mysql_config’ would actually be called ‘mysql_config5’, and in this case, you would have to do this after installing: export PATH=$PATH:/opt/local/lib/mysql5/bin. You can find more details here.

Note1: I’ve seen some people saying that installing python-dev and libmysqlclient-dev also helped, however I do not know if these packages are available on Mac OS.

Note2: Also, make sure to try running the commands as root.

I got my answers from (besides my brain) these places (maybe you could have a look at them, to see if it would help): 1, 2, 3, 4.

I hoped I helped, and would be happy to know if any of this worked, or not. Good luck.


回答 1

我一直在调试此问题-3小时17分钟。令我特别烦恼的是,我已经通过先前的uni工作在系统上安装了sql,但是pip / pip3却无法识别它。这些线程以及我在互联网上搜索过的许多其他线程有助于消除问题,但实际上并没有解决问题。

回答

Pip在相对于Macintosh HD @ 的Homebrew目录中寻找mysql二进制文件

/usr/local/Cellar/

所以我发现这需要您进行一些更改

步骤1:如果尚未下载MySql,请下载https://dev.mysql.com/downloads/

步骤2:相对于Macintosh HD和cd 找到它

/usr/local/mysql/bin

第3步:打开终端并使用所选的文本编辑器后-我本人是个Neovim家伙,所以我打了一下(Mac并不会自动附带……另一天的故事)

nvim mysql_config

第4步:您将在第112行看到

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

改成

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

*您会注意到该文件具有只读访问权限,因此,如果您使用vim或neovim

:w !sudo tee %

步骤5:转到主目录并编辑.bash_profile文件

cd ~

然后

nvim .bash_profile

并添加

export PATH="/usr/local/mysql/bin:$PATH"

到文件然后保存

步骤6:相对于Macintosh HD找到路径并将其添加

cd /private/etc/

然后

nvim paths

并添加

/usr/local/mysql/bin

*您会再次注意到该文件具有只读访问权限,因此,如果您使用vim或neovim

:w !sudo tee % 

然后

cd ~

然后通过运行您的更改刷新终端

source .bash_profile

最后

pip3 install mysqlclient

还有中提琴 记住这是一种氛围。

I had been debugging this problem forever – 3 hours 17 mins. What particularly annoyed me was that I already had sql installed on my system through prior uni work but pip/pip3 wasn’t recognising it. These threads above and many other I scoured the internet for were helpful in eluminating the problem but didn’t actually solve things.

ANSWER

Pip is looking for mysql binaries in the Homebrew Directory which is located relative to Macintosh HD @

/usr/local/Cellar/

so I found that this requires you making a few changes

step 1: Download MySql if not already done so https://dev.mysql.com/downloads/

Step 2: Locate it relative to Macintosh HD and cd

/usr/local/mysql/bin

Step 3: Once there open terminal and use a text editor of choice – I’m a neovim guy myself so I typed (doesn’t automatically come with Mac… another story for another day)

nvim mysql_config

Step 4: You will see at approx line 112

# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

Change to

# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

*you’ll notice that this file has read-only access so if your using vim or neovim

:w !sudo tee %

Step 5: Head to the home directory and edit the .bash_profile file

cd ~

Then

nvim .bash_profile

and add

export PATH="/usr/local/mysql/bin:$PATH"

to the file then save

Step 6: relative to Macintosh HD locate paths and add to it

cd /private/etc/

then

nvim paths

and add

/usr/local/mysql/bin

*you’ll again notice that this file has read-only access so if your using vim or neovim

:w !sudo tee % 

then

cd ~

then refresh the terminal with your changes by running

source .bash_profile

Finally

pip3 install mysqlclient

And Viola. Remember it’s a vibe.


回答 2

如果您不想安装完整的mysql,我们可以通过安装mysqlclient来解决此问题。cmd brew install mysqlclient 完成后,将要求在下面添加以下行~/.bash_profile

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

关闭终端并启动新终端,然后继续 pip install mysqlclient

If you don’t want to install full mysql, we can fix this by just installing mysqlclient brew install mysqlclient Once cmd is completed it will ask to add below line to ~/.bash_profile:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

Close terminal and start new terminal and proceed with pip install mysqlclient


回答 3

我在MacOS Catalina上运行Python 3.6。我的问题是我尝试安装mysqlclient==1.4.2.post1,并不断抛出mysql_config not found错误。

这是我解决问题的步骤。

  1. 使用brew安装mysql-connector-c(如果已经有mysql,请先安装unlink brew unlink mysql)-brew install mysql-connector-c
  2. 打开mysql_config并编辑第112行附近的文件
# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl -这将为您提供更多有关将openssl放入PATH中需要执行的操作的信息
  2. 关于步骤3,您需要执行以下操作将openssl放入PATH中- echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
  3. 供编译器找到openssl- export LDFLAGS="-L/usr/local/opt/openssl/lib"
  4. 供编译器找到openssl- export CPPFLAGS="-I/usr/local/opt/openssl/include"

I am running Python 3.6 on MacOS Catalina. My issue was that I tried to install mysqlclient==1.4.2.post1 and it keeps throwing mysql_config not found error.

This is the steps I took to solve the issue.

  1. Install mysql-connector-c using brew (if you have mysql already install unlink first brew unlink mysql) – brew install mysql-connector-c
  2. Open mysql_config and edit the file around line 112
# Create options 
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
  1. brew info openssl – this will give you more information on what needs to be done about putting openssl in PATH
  2. in relation to step 3, you need to do this to put openssl in PATH – echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
  3. for compilers to find openssl – export LDFLAGS="-L/usr/local/opt/openssl/lib"
  4. for compilers to find openssl – export CPPFLAGS="-I/usr/local/opt/openssl/include"

回答 4

当我安装mysqlclient时也会发生这种情况,

$ pip install mysqlclient

如用户3429036所说,

$ brew install mysql

Also this happens when I was installing mysqlclient,

$ pip install mysqlclient

As user3429036 said,

$ brew install mysql

回答 5

此答案适用于brew不是从官方安装而是从官方安装的MacOS用户.dmg/.pkg。该安装程序无法编辑您的PATH,从而导致出现问题:

  1. 所有MySQL命令一样mysqlmysqladminmysql_config,等不能被发现,并作为一个结果:
  2. “MySQL的预置面板”未出现在系统偏好设置,
  3. 您不能安装任何与MySQL通信的API,包括 mysqlclient

您要做的是附加MySQL bin文件夹(通常/usr/local/mysql/bin在您PATH~/.bash_profile文件中添加以下行:

export PATH="/usr/local/mysql/bin/:$PATH"

然后,您应该重新加载您~/.bash_profile的更改,以使更改在当前的终端会话中生效:

source ~/.bash_profile

mysqlclient但是,在安装之前,您需要接受XcodeBuild许可证:

sudo xcodebuild -license

按照他们的指示退出您的家人,之后您应该可以顺利安装mysqlclient

pip install mysqlclient

安装在此之后,你必须做一件事来解决运行时错误,船舶与MySQL(动态库libmysqlclient.dylib未找到),加入这行到你的系统动态库路径:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH

This answer is for MacOS users who did not install from brew but rather from the official .dmg/.pkg. That installer fails to edit your PATH, causing things to break out of the box:

  1. All MySQL commands like mysql, mysqladmin, mysql_config, etc cannot be found, and as a result:
  2. the “MySQL Preference Pane” fails to appear in System Preferences, and
  3. you cannot install any API that communicates with MySQL, including mysqlclient

What you have to do is appending the MySQL bin folder (typically /usr/local/mysql/bin in your PATH by adding this line in your ~/.bash_profile file:

export PATH="/usr/local/mysql/bin/:$PATH"

You should then reload your ~/.bash_profile for the change to take effect in your current Terminal session:

source ~/.bash_profile

Before installing mysqlclient, however, you need to accept the XcodeBuild license:

sudo xcodebuild -license

Follow their directions to sign away your family, after which you should be able to install mysqlclient without issue:

pip install mysqlclient

After installing that, you must do one more thing to fix a runtime bug that ships with MySQL (Dynamic Library libmysqlclient.dylib not found), by adding this line to your system dynamic libraries path:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH


回答 6

如果您通过指定版本使用Homebrew安装了mysql ,则mysql_config将出现在此处。– /usr/local/Cellar/mysql@5.6/5.6.47/bin

您可以在/ usr / local /目录中使用ls命令找到sql bin的路径

/usr/local/Cellar/mysql@5.6/5.6.47/bin

像这样将路径添加到bash配置文件。

nano ~/.bash_profile

export PATH="/usr/local/Cellar/mysql@5.6/5.6.47/bin:$PATH"

If you have installed mysql using Homebrew by specifying a version then mysql_config would be present here. – /usr/local/Cellar/mysql@5.6/5.6.47/bin

you can find the path of the sql bin by using ls command in /usr/local/ directory

/usr/local/Cellar/mysql@5.6/5.6.47/bin

Add the path to bash profile like this.

nano ~/.bash_profile

export PATH="/usr/local/Cellar/mysql@5.6/5.6.47/bin:$PATH"

回答 7

在我的情况下,问题是我在python虚拟环境中运行命令,尽管我将其放在.bash_profile文件中,但它没有/ usr / local / mysql / bin的路径。只是在虚拟环境中导出路径对我有用。

对于您的信息,sql_config位于bin目录中。

The problem in my case was that I was running the command inside a python virtual environment and it didn’t had the path to /usr/local/mysql/bin though I have put it in the .bash_profile file. Just exporting the path in the virtual env worked for me.

For your info sql_config resides inside bin directory.


回答 8

对我来说,安装brew或apt-get也不容易,所以我通过以下网址下载了mysql:https//dev.mysql.com/downloads/connector/python/,并安装了它。因此,我可以在以下目录中找到mysql_config:/ usr / local / mysql / bin

下一步是:

  1. 导出PATH = $ PATH:/ usr / local / mysql / bin
  2. 点安装MySQL-python == 1.2.5

Install brew or apt-get is also not easy for me so I downloaded mysql via: https://dev.mysql.com/downloads/connector/python/, installed it. So I can find mysql_config int this directory: /usr/local/mysql/bin

the next step is:

  1. export PATH=$PATH:/usr/local/mysql/bin
  2. pip install MySQL-python==1.2.5

Django模型“未声明显式的app_label”

问题:Django模型“未声明显式的app_label”

我机智的结束了。经过十多个小时的故障排除(可能还有更多),我认为自己终于可以从事业务了,但是后来我得到了:

Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label 

网络上对此信息很少,目前还没有解决方案解决了我的问题。任何建议将不胜感激。

我正在使用Python 3.4和Django 1.10。

在我的settings.py中:

INSTALLED_APPS = [
    'DeleteNote.apps.DeletenoteConfig',
    'LibrarySync.apps.LibrarysyncConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我的apps.py文件如下所示:

from django.apps import AppConfig


class DeletenoteConfig(AppConfig):
    name = 'DeleteNote'

from django.apps import AppConfig


class LibrarysyncConfig(AppConfig):
    name = 'LibrarySync'

I’m at wit’s end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:

Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label 

There is SO LITTLE info on this on the web, and no solution out there has resolved my issue. Any advice would be tremendously appreciated.

I’m using Python 3.4 and Django 1.10.

From my settings.py:

INSTALLED_APPS = [
    'DeleteNote.apps.DeletenoteConfig',
    'LibrarySync.apps.LibrarysyncConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

And my apps.py files look like this:

from django.apps import AppConfig


class DeletenoteConfig(AppConfig):
    name = 'DeleteNote'

and

from django.apps import AppConfig


class LibrarysyncConfig(AppConfig):
    name = 'LibrarySync'

回答 0

您是否缺少将应用程序名称放入设置文件中的信息?这myAppNameConfig.manage.py createapp myAppName命令在apps.py生成的默认类。其中myAppName是您的应用程序的名称。

settings.py

INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

这样,设置文件可以找出您要调用的应用程序。您可以通过在以下代码中添加以下代码来更改apps.py文件中的外观:

myAppName / apps.py

class myAppNameConfig(AppConfig):
    name = 'myAppName'
    verbose_name = 'A Much Better Name'

Are you missing putting in your application name into the settings file? The myAppNameConfig is the default class generated at apps.py by the .manage.py createapp myAppName command. Where myAppName is the name of your app.

settings.py

INSTALLED_APPS = [
'myAppName.apps.myAppNameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

This way, the settings file finds out what you want to call your application. You can change how it looks later in the apps.py file by adding the following code in

myAppName/apps.py

class myAppNameConfig(AppConfig):
    name = 'myAppName'
    verbose_name = 'A Much Better Name'

回答 1

我收到相同的错误,但我不知道如何解决此问题。我花了很多时间才注意到,我有一个与django的manage.py相同的initcopy文件。

之前:

|-- myproject
  |-- __init__.py
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

后:

|-- myproject
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

您会收到此“未声明显式app_label”错误,这非常令人困惑。但是删除此初始化文件解决了我的问题。

I get the same error and I don´t know how to figure out this problem. It took me many hours to notice that I have a init.py at the same direcory as the manage.py from django.

Before:

|-- myproject
  |-- __init__.py
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

After:

|-- myproject
  |-- manage.py
  |-- myproject
    |-- ...
  |-- app1
    |-- models.py
  |-- app2
    |-- models.py

It is quite confused that you get this “doesn’t declare an explicit app_label” error. But deleting this init file solved my problem.


回答 2

使用PyCharm运行测试时,我遇到了完全相同的错误。我已经通过显式设置DJANGO_SETTINGS_MODULE环境变量来修复它。如果您使用的是PyCharm,只需点击编辑配置按钮,然后选择环境变量

将变量设置为your_project_name.settings,这应该可以解决问题。

似乎发生此错误,因为PyCharm使用自己的运行测试manage.py

I had exactly the same error when running tests with PyCharm. I’ve fixed it by explicitly setting DJANGO_SETTINGS_MODULE environment variable. If you’re using PyCharm, just hit Edit Configurations button and choose Environment Variables.

Set the variable to your_project_name.settings and that should fix the thing.

It seems like this error occurs, because PyCharm runs tests with its own manage.py.


回答 3

我在使用时得到了这个,./manage.py shell 然后我不小心从根项目级别目录中导入了

# don't do this
from project.someapp.someModule import something_using_a_model
# do this
from someapp.someModule import something_using_a_model

something_using_a_model()

I got this one when I used ./manage.py shell then I accidentally imported from the root project level directory

# don't do this
from project.someapp.someModule import something_using_a_model
# do this
from someapp.someModule import something_using_a_model

something_using_a_model()

回答 4

作为使用Python3的菜鸟,我发现它可能是导入错误而不是Django错误

错误:

from someModule import someClass

对:

from .someModule import someClass

这种情况发生在几天前,但我真的无法复制它…我认为只有Django新手才可能遇到这种情况。

尝试在admin.py中注册模型:

from django.contrib import admin
from user import User
admin.site.register(User)

尝试运行服务器,错误看起来像这样

some lines...
File "/path/to/admin.py" ,line 6
tell you there is an import error
some lines...
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label

更改user.user,问题已解决

as a noob using Python3 ,I find it might be an import error instead of a Django error

wrong:

from someModule import someClass

right:

from .someModule import someClass

this happens a few days ago but I really can’t reproduce it…I think only people new to Django may encounter this.here’s what I remember:

try to register a model in admin.py:

from django.contrib import admin
from user import User
admin.site.register(User)

try to run server, error looks like this

some lines...
File "/path/to/admin.py" ,line 6
tell you there is an import error
some lines...
Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label

change user to .user ,problem solved


回答 5

我刚才有同样的问题。我通过在应用程序名称上添加命名空间来修复我的问题。希望有人觉得这有帮助。

apps.py

from django.apps import AppConfig    

class SalesClientConfig(AppConfig):
        name = 'portal.sales_client'
        verbose_name = 'Sales Client'

I had the same problem just now. I’ve fixed mine by adding a namespace on the app name. Hope someone find this helpful.

apps.py

from django.apps import AppConfig    

class SalesClientConfig(AppConfig):
        name = 'portal.sales_client'
        verbose_name = 'Sales Client'

回答 6

我在测试中导入模型时遇到此错误,即鉴于此Django项目结构:

|-- myproject
    |-- manage.py
    |-- myproject
    |-- myapp
        |-- models.py  # defines model: MyModel
        |-- tests
            |-- test_models.py

test_models.pyMyModel以这种方式导入的文件中:

from models import MyModel

如果以这种方式导入,则问题已解决:

from myapp.models import MyModel

希望这可以帮助!

PS:也许这有点晚了,但是在我的代码中没有其他人找到如何解决此问题的答案,我想分享我的解决方案。

I got this error on importing models in tests, i.e. given this Django project structure:

|-- myproject
    |-- manage.py
    |-- myproject
    |-- myapp
        |-- models.py  # defines model: MyModel
        |-- tests
            |-- test_models.py

in file test_models.py I imported MyModel in this way:

from models import MyModel

The problem was fixed if it is imported in this way:

from myapp.models import MyModel

Hope this helps!

PS: Maybe this is a bit late, but I not found in others answers how to solve this problem in my code and I want to share my solution.


回答 7

在继续遇到这个问题并继续回到这个问题之后,我想我会分享我的问题所在。

@Xeberdee正确的所有内容,请按照以下说明操作,看看是否可以解决问题,如果不是,这是我的问题:

在我的apps.py中,这就是我拥有的:

class AlgoExplainedConfig(AppConfig):
    name = 'algo_explained'
    verbose_name = "Explain_Algo"
    ....

我所做的就是在我的应用名称之前添加了项目名称,如下所示:

class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"

这样就解决了我的问题,之后我就可以运行makemigrations和migration命令!祝好运

After keep on running into this issue and keep on coming back to this question I thought I’d share what my problem was.

Everything that @Xeberdee is correct so follow that and see if that solves the issue, if not this was my issue:

In my apps.py this is what I had:

class AlgoExplainedConfig(AppConfig):
    name = 'algo_explained'
    verbose_name = "Explain_Algo"
    ....

And all I did was I added the project name in front of my app name like this:

class AlgoExplainedConfig(AppConfig):
name = '**algorithms_explained**.algo_explained'
verbose_name = "Explain_Algo"

and that solved my problem and I was able to run the makemigrations and migrate command after that! good luck


回答 8

我今天在尝试运行Django测试时遇到此错误,因为我from .models import *在其中一个文件中使用了速记语法。问题是我的文件结构如下:

    apps/
      myapp/
        models/
          __init__.py
          foo.py
          bar.py

models/__init__.py我使用速记语法导入模型时:

    from .foo import *
    from .bar import *

在我的应用程序中,我正在导入如下模型:

    from myapp.models import Foo, Bar

这导致了Django model doesn't declare an explicit app_label运行时./manage.py test

要解决此问题,我必须从完整路径中显式导入models/__init__.py

    from myapp.models.foo import *
    from myapp.models.bar import *

那解决了错误。

H / t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a

I had this error today trying to run Django tests because I was using the shorthand from .models import * syntax in one of my files. The issue was that I had a file structure like so:

    apps/
      myapp/
        models/
          __init__.py
          foo.py
          bar.py

and in models/__init__.py I was importing my models using the shorthand syntax:

    from .foo import *
    from .bar import *

In my application I was importing models like so:

    from myapp.models import Foo, Bar

This caused the Django model doesn't declare an explicit app_label when running ./manage.py test.

To fix the problem, I had to explicitly import from the full path in models/__init__.py:

    from myapp.models.foo import *
    from myapp.models.bar import *

That took care of the error.

H/t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a


回答 9

就我而言,这是因为我在项目级urls.py中使用了相对模块路径,INSTALLED_APPSapps.py不是植根于项目根目录中。即,绝对模块路径始终存在,而不是相对模块路径+ hack。

无论我在应用程序中INSTALLED_APPSapps.py应用程序中弄乱了多少,我都无法同时使用它们runserverpytest直到这三个都植根于项目根目录为止。

资料夹结构:

|-- manage.py
|-- config
    |-- settings.py
    |-- urls.py
|-- biz_portal
    |-- apps
        |-- portal
            |-- models.py
            |-- urls.py
            |-- views.py
            |-- apps.py

用下面的,我可以运行manage.py runserver,并与WSGI和使用gunicorn portal应用看法,并没有麻烦,但pytest将与错误ModuleNotFoundError: No module named 'apps',尽管DJANGO_SETTINGS_MODULE被正确配置。

config / settings.py:

INSTALLED_APPS = [
    ...
    "apps.portal.apps.PortalConfig",
]

biz_portal / apps / portal / apps.py:

class PortalConfig(AppConfig):
    name = 'apps.portal'

config / urls.py:

urlpatterns = [
    path('', include('apps.portal.urls')),
    ...
]

改变应用程序的参考配置/ settings.py来biz_portal.apps.portal.apps.PortalConfigPortalConfig.namebiz_portal.apps.portal允许pytest来运行(我没有做检查portal的意见还),但runserver将与错误

RuntimeError:模型类apps.portal.models.Business没有声明显式的app_label,并且不在INSTALLED_APPS中的应用程序中

最后,我摸索apps.portal着查看仍然在使用相对路径的内容,并发现config / urls.py也应该使用biz_portal.apps.portal.urls

In my case, this was happening because I used a relative module path in project-level urls.py, INSTALLED_APPS and apps.py instead of being rooted in the project root. i.e. absolute module paths throughout, rather than relative modules paths + hacks.

No matter how much I messed with the paths in INSTALLED_APPS and apps.py in my app, I couldn’t get both runserver and pytest to work til all three of those were rooted in the project root.

Folder structure:

|-- manage.py
|-- config
    |-- settings.py
    |-- urls.py
|-- biz_portal
    |-- apps
        |-- portal
            |-- models.py
            |-- urls.py
            |-- views.py
            |-- apps.py

With the following, I could run manage.py runserver and gunicorn with wsgi and use portal app views without trouble, but pytest would error with ModuleNotFoundError: No module named 'apps' despite DJANGO_SETTINGS_MODULE being configured correctly.

config/settings.py:

INSTALLED_APPS = [
    ...
    "apps.portal.apps.PortalConfig",
]

biz_portal/apps/portal/apps.py:

class PortalConfig(AppConfig):
    name = 'apps.portal'

config/urls.py:

urlpatterns = [
    path('', include('apps.portal.urls')),
    ...
]

Changing the app reference in config/settings.py to biz_portal.apps.portal.apps.PortalConfig and PortalConfig.name to biz_portal.apps.portal allowed pytest to run (I don’t have tests for portal views yet) but runserver would error with

RuntimeError: Model class apps.portal.models.Business doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS

Finally I grepped for apps.portal to see what’s still using a relative path, and found that config/urls.py should also use biz_portal.apps.portal.urls.


回答 10

当我尝试为单个应用生成迁移时遇到了此错误,该应用由于git合并而存在现有的格式错误的迁移。例如

manage.py makemigrations myapp

当我删除它的迁移然后运行:

manage.py makemigrations

不会发生该错误,并且迁移成功生成。

I ran into this error when I tried generating migrations for a single app which had existing malformed migrations due to a git merge. e.g.

manage.py makemigrations myapp

When I deleted it’s migrations and then ran:

manage.py makemigrations

the error did not occur and the migrations generated successfully.


回答 11

我有一个类似的问题,但是我可以通过在模型类中使用Meta类明确指定app_label来解决我的问题

class Meta:
    app_label  = 'name_of_my_app'

I had a similar issue, but I was able to solve mine by specifying explicitly the app_label using Meta Class in my models class

class Meta:
    app_label  = 'name_of_my_app'

回答 12

尝试将Django Rest Framework应用程序升级到DRF 3.6.3和Django 1.11.1时出现此错误。

对于这种情况下的其他人,我在GitHub问题中找到了解决方案,该问题UNAUTHENTICATED_USERDRF设置中取消设置

# webapp/settings.py
...
REST_FRAMEWORK = {
    ...
    'UNAUTHENTICATED_USER': None
    ...
}

I got this error while trying to upgrade my Django Rest Framework app to DRF 3.6.3 and Django 1.11.1.

For anyone else in this situation, I found my solution in a GitHub issue, which was to unset the UNAUTHENTICATED_USER setting in the DRF settings:

# webapp/settings.py
...
REST_FRAMEWORK = {
    ...
    'UNAUTHENTICATED_USER': None
    ...
}

回答 13

我只是遇到了这个问题,并弄清楚出了什么问题。由于以前没有答案描述发生在我身上的问题,因此我将其发布给其他人:

  • 问题出python migrate.py startapp myApp在我的项目根文件夹中,然后将myApp移到一个子文件夹中mv myApp myFolderWithApps/
  • 我写了myApp.models并运行 python migrate.py makemigrations。一切顺利。
  • 然后我对另一个从myApp导入模型的应用做了同样的操作。b!执行迁移时,我遇到了这个错误。那是因为我不得不使用myFolderWithApps.myApp引用我的应用程序,但是我却忘记了更新MyApp / apps.py。因此,我在第二个应用程序中更正了myApp / apps.py,设置/ INSTALLED_APPS和导入路径。
  • 但是随后错误不断发生:原因是我进行了迁移,试图使用错误的路径从myApp导入模型。我试图更正迁移文件,但我发现重置数据库和删除迁移从头开始更容易。

简而言之:-问题最初是由于myApp的apps.py,第二个应用程序的设置和导入路径中错误的应用程序名称引起的。-但这不足以更正这三个位置的路径,因为迁移是使用导入引用了错误的应用程序名称创建的。因此,相同的错误在迁移过程中始终发生(这次迁移除外)。

所以…请检查您的迁移情况,祝您好运!

I just ran into this issue and figured out what was going wrong. Since no previous answer described the issue as it happened to me, I though I would post it for others:

  • the issue came from using python migrate.py startapp myApp from my project root folder, then move myApp to a child folder with mv myApp myFolderWithApps/.
  • I wrote myApp.models and ran python migrate.py makemigrations. All went well.
  • then I did the same with another app that was importing models from myApp. Kaboom! I ran into this error, while performing makemigrations. That was because I had to use myFolderWithApps.myApp to reference my app, but I had forgotten to update MyApp/apps.py. So I corrected myApp/apps.py, settings/INSTALLED_APPS and my import path in my second app.
  • but then the error kept happening: the reason was that I had migrations trying to import the models from myApp with the wrong path. I tried to correct the migration file, but I went at the point where it was easier to reset the DB and delete the migrations to start from scratch.

So to make a long story short: – the issue was initially coming from the wrong app name in apps.py of myApp, in settings and in the import path of my second app. – but it was not enough to correct the paths in these three places, as migrations had been created with imports referencing the wrong app name. Therefore, the same error kept happening while migrating (except this time from migrations).

So… check your migrations, and good luck!


回答 14

在Django rest_framework中构建API时遇到类似的错误。

RuntimeError:模型类apps.core.models.University未声明显式> app_label,并且不在INSTALLED_APPS中的应用程序中。

luke_aus的答案通过纠正我的问题帮助了我 urls.py

from project.apps.views import SurgeryView

from apps.views import SurgeryView

I’ve got a similar error while building an API in Django rest_framework.

RuntimeError: Model class apps.core.models.University doesn’t declare an explicit > app_label and isn’t in an application in INSTALLED_APPS.

luke_aus’s answer helped me by correcting my urls.py

from

from project.apps.views import SurgeryView

to

from apps.views import SurgeryView

回答 15

就我而言,将代码从Django 1.11.11移植到Django 2.2时出现此错误。我正在定义一个自定义FileSystemStorage派生类。在Django 1.11.11中,我在models.py中包含以下行:

from django.core.files.storage import Storage, DefaultStorage

后来在文件中,我有了类定义:

class MyFileStorage(FileSystemStorage):

但是,在Django 2.2中,我需要FileSystemStorage在导入时显式引用类:

from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage

和贴吧,错误消失了。

请注意,每个人都在报告Django服务器随地吐痰的错误消息的最后一部分。但是,如果向上滚动,则会在该错误的中间部分找到原因。

In my case I got this error when porting code from Django 1.11.11 to Django 2.2. I was defining a custom FileSystemStorage derived class. In Django 1.11.11 I was having the following line in models.py:

from django.core.files.storage import Storage, DefaultStorage

and later in the file I had the class definition:

class MyFileStorage(FileSystemStorage):

However, in Django 2.2 I need to explicitly reference FileSystemStorage class when importing:

from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage

and voilà!, the error dissapears.

Note, that everyone is reporting the last part of the error message spitted by Django server. However, if you scroll up you will find the reason in the middle of that error mambo-jambo.


回答 16

就我而言,我能够找到一个修复程序,并且通过查看其他人的代码也可能是相同的问题。文件。

希望这对某人有帮助。这是我对编码社区的第一个贡献

in my case I was able to find a fix and by looking at the everyone else’s code it may be the same issue.. I simply just had to add ‘django.contrib.sites’ to the list of installed apps in the settings.py file.

hope this helps someone. this is my first contribution to the coding community


回答 17

TL; DR:添加空白__init__.py为我解决了此问题。

我在PyCharm中遇到此错误,并意识到我的设置文件根本没有被导入。没有明显的错误告诉我,但是当我在settings.py中放入一些废话时,它没有引起错误。

我在local_settings文件夹中有settings.py。但是,我希望在同一文件夹中包含__init__.py以便导入。一旦我添加了这个,错误就消失了。

TL;DR: Adding a blank __init__.py fixed the issue for me.

I got this error in PyCharm and realised that my settings file was not being imported at all. There was no obvious error telling me this, but when I put some nonsense code into the settings.py, it didn’t cause an error.

I had settings.py inside a local_settings folder. However, I’d fogotten to include a __init__.py in the same folder to allow it to be imported. Once I’d added this, the error went away.


回答 18

如果所有配置正确,则可能只是导入混乱。密切关注您如何导入违规模型。

以下无效from .models import Business。请使用完整的导入路径:from myapp.models import Business

If you have got all the config right, it might just be an import mess. keep an eye on how you are importing the offending model.

The following won’t work from .models import Business. Use full import path instead: from myapp.models import Business


回答 19

如果所有其他方法均失败,并且在尝试导入PyCharm“ Python控制台”(或“ Django控制台”)时遇到此错误:

尝试重新启动控制台。

这真是令人尴尬,但是花了我一段时间我才意识到自己忘记这样做了。

这是发生了什么:

添加了一个新的应用程序,然后添加了一个最小模型,然后尝试在Python / Django控制台中导入模型(PyCharm pro 2019.2)。这引发了doesn't declare an explicit app_label错误,因为我没有将新应用添加到中INSTALLED_APPS。因此,我将应用添加到了INSTALLED_APPS,再次尝试了导入,但是仍然遇到相同的错误。

来到这里,阅读所有其他答案,但似乎没有合适的答案。

最后,令我惊讶的是,在将新应用添加到后,我还没有重新启动Python控制台INSTALLED_APPS

注意:在将新对象添加到模块后,无法重新启动PyCharm Python控制台也是一种使人非常困惑的好方法 ImportError: Cannot import name ...

If all else fails, and if you are seeing this error while trying to import in a PyCharm “Python console” (or “Django console”):

Try restarting the console.

This is pretty embarassing, but it took me a while before I realized I had forgotten to do that.

Here’s what happened:

Added a fresh app, then added a minimal model, then tried to import the model in the Python/Django console (PyCharm pro 2019.2). This raised the doesn't declare an explicit app_label error, because I had not added the new app to INSTALLED_APPS. So, I added the app to INSTALLED_APPS, tried the import again, but still got the same error.

Came here, read all the other answers, but nothing seemed to fit.

Finally it hit me that I had not yet restarted the Python console after adding the new app to INSTALLED_APPS.

Note: failing to restart the PyCharm Python console, after adding a new object to a module, is also a great way to get a very confusing ImportError: Cannot import name ...


回答 20

O … M … G我也遇到了这个错误,我花了将近2天的时间,现在终于设法解决了。老实说…错误与问题无关。就我而言,这只是语法问题。我试图独立运行一个在django上下文中使用某些django模型的python模块,但该模块本身不是django模型。但是我宣布全班不对

而不是

class Scrapper:
    name = ""
    main_link= ""
    ...

我在做

class Scrapper(Website):
    name = ""
    main_link= ""
    ...

这显然是错误的。该消息是如此令人误解,以至于我忍不住想,但我认为这是配置方面的问题,或者只是以错误的方式使用django,因为我对此很陌生。

我将在这里与新手分享,因为我经历同样的愚蠢期盼能解决他们的问题。

O…M…G I was getting this error too and I spent almost 2 days on it and now I finally managed to solve it. Honestly…the error had nothing to do with what the problem was. In my case it was a simple matter of syntax. I was trying to run a python module standalone that used some django models in a django context, but the module itself wasn’t a django model. But I was declaring the class wrong

instead of having

class Scrapper:
    name = ""
    main_link= ""
    ...

I was doing

class Scrapper(Website):
    name = ""
    main_link= ""
    ...

which is obviously wrong. The message is so misleading that I couldn’t help myself but think it was some issue with configuration or just using django in a wrong way since I’m very new to it.

I’ll share this here for someone newbie as me going through the same silliness can hopefully solve their issue.


回答 21

SECRET_KEY从环境变量中移出并在运行应用程序时忘记对其进行设置后,收到了此错误。如果你有这样的事情settings.py

SECRET_KEY = os.getenv('SECRET_KEY')

然后确保您实际上在设置环境变量。

I received this error after I moved the SECRET_KEY to pull from an environment variable and forgot to set it when running the application. If you have something like this in your settings.py

SECRET_KEY = os.getenv('SECRET_KEY')

then make sure you are actually setting the environment variable.


回答 22

很可能您有依赖进口

在我的情况下,我在模型中使用了序列化程序类作为参数,而序列化程序类则在使用以下模型:serializer_class = AccountSerializer

from ..api.serializers import AccountSerializer

class Account(AbstractBaseUser):
    serializer_class = AccountSerializer
    ...

并在“序列化器”文件中:

from ..models import Account

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = (
            'id', 'email', 'date_created', 'date_modified',
            'firstname', 'lastname', 'password', 'confirm_password')
    ...

Most probably you have dependent imports.

In my case I used a serializer class as a parameter in my model, and the serializer class was using this model: serializer_class = AccountSerializer

from ..api.serializers import AccountSerializer

class Account(AbstractBaseUser):
    serializer_class = AccountSerializer
    ...

And in the “serializers” file:

from ..models import Account

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = (
            'id', 'email', 'date_created', 'date_modified',
            'firstname', 'lastname', 'password', 'confirm_password')
    ...

回答 23

我今天遇到了这个错误,并在谷歌搜索后最终到达了这里。现有的答案似乎都与我的情况无关。我唯一需要做的就是从__init__.py应用程序顶层的文件中导入模型。我必须使用模型将导入移动到函数中。

Django似乎有一些奇怪的代码,在许多不同的情况下都可能会失败!

I got this error today and ended up here after googling. None of the existing answers seem relevant to my situation. The only thing I needed to do was to import a model from my __init__.py file in the top level of an app. I had to move my imports into the functions using the model.

Django seems to have some weird code that can fail like this in so many different scenarios!


回答 24

我今天也遇到了这个错误。该消息引用了INSTALLED_APPS我的应用程序中的某些特定应用程序。但实际上,它与此特定的应用程序无关。我使用了一个新的虚拟环境,却忘记安装一些在该项目中使用的库。在我安装了附加库之后,它开始工作了。

I got this error also today. The Message referenced to some specific app of my apps in INSTALLED_APPS. But in fact it had nothing to do with this specific App. I used a new virtual Environment and forgot to install some Libraries, that i used in this project. After i installed the additional Libraries, it worked.


回答 25

对于PyCharm用户:使用“不干净”的项目结构时出现错误。

是:

project_root_directory
└── src
    ├── chat
       ├── migrations
       └── templates
    ├── django_channels
    └── templates

现在:

project_root_directory
├── chat
   ├── migrations
   └── templates
       └── chat
├── django_channels
└── templates

这里有很多好的解决方案,但是我认为,首先,您应该在设置之前清理项目结构或调整PyCharm Django设置 DJANGO_SETTINGS_MODULE变量等。

希望对别人有帮助。干杯。

For PyCharm users: I had an error using not “clean” project structure.

Was:

project_root_directory
└── src
    ├── chat
    │   ├── migrations
    │   └── templates
    ├── django_channels
    └── templates

Now:

project_root_directory
├── chat
│   ├── migrations
│   └── templates
│       └── chat
├── django_channels
└── templates

Here is a lot of good solutions, but I think, first of all, you should clean your project structure or tune PyCharm Django settings before setting DJANGO_SETTINGS_MODULE variables and so on.

Hope it’ll help someone. Cheers.


回答 26

问题是:

  1. 您已经对模型文件进行了修改,但尚未将其添加到数据库中,但是您正在尝试运行Python manage.py runserver。

  2. 运行Python manage.py makemigrations

  3. Python manage.py迁移

  4. 现在,Python manage.py runserver和一切都应该没问题。

The issue is that:

  1. You have made modifications to your models file, but not addedd them yet to the DB, but you are trying to run Python manage.py runserver.

  2. Run Python manage.py makemigrations

  3. Python manage.py migrate

  4. Now Python manage.py runserver and all should be fine.


在Django网站中将HTML渲染为PDF

问题:在Django网站中将HTML渲染为PDF

对于我的django网站,我正在寻找一种将动态html页面转换为pdf的简单解决方案。

页面包含HTML和来自Google可视化API的图表(该图表基于javascript,但必须包含这些图表)。

For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.

Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).


回答 0

尝试从Reportlab解决方案。

下载并像往常一样使用python setup.py install安装

您还需要安装以下模块:具有easy_install的xhtml2pdf,html5lib,pypdf。

这是一个用法示例:

首先定义此功能:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

然后,您可以像这样使用它:

def myview(request):
    #Retrieve data or whatever you need
    return render_to_pdf(
            'mytemplate.html',
            {
                'pagesize':'A4',
                'mylist': results,
            }
        )

模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>My Title</title>
        <style type="text/css">
            @page {
                size: {{ pagesize }};
                margin: 1cm;
                @frame footer {
                    -pdf-frame-content: footerContent;
                    bottom: 0cm;
                    margin-left: 9cm;
                    margin-right: 9cm;
                    height: 1cm;
                }
            }
        </style>
    </head>
    <body>
        <div>
            {% for item in mylist %}
                RENDER MY CONTENT
            {% endfor %}
        </div>
        <div id="footerContent">
            {%block page_foot%}
                Page <pdf:pagenumber>
            {%endblock%}
        </div>
    </body>
</html>

希望能帮助到你。

Try the solution from Reportlab.

Download it and install it as usual with python setup.py install

You will also need to install the following modules: xhtml2pdf, html5lib, pypdf with easy_install.

Here is an usage example:

First define this function:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

Then you can use it like this:

def myview(request):
    #Retrieve data or whatever you need
    return render_to_pdf(
            'mytemplate.html',
            {
                'pagesize':'A4',
                'mylist': results,
            }
        )

The template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>My Title</title>
        <style type="text/css">
            @page {
                size: {{ pagesize }};
                margin: 1cm;
                @frame footer {
                    -pdf-frame-content: footerContent;
                    bottom: 0cm;
                    margin-left: 9cm;
                    margin-right: 9cm;
                    height: 1cm;
                }
            }
        </style>
    </head>
    <body>
        <div>
            {% for item in mylist %}
                RENDER MY CONTENT
            {% endfor %}
        </div>
        <div id="footerContent">
            {%block page_foot%}
                Page <pdf:pagenumber>
            {%endblock%}
        </div>
    </body>
</html>

Hope it helps.


回答 1

https://github.com/nigma/django-easy-pdf

模板:

{% extends "easy_pdf/base.html" %}

{% block content %}
    <div id="content">
        <h1>Hi there!</h1>
    </div>
{% endblock %}

视图:

from easy_pdf.views import PDFTemplateView

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"

如果要在Python 3上使用django-easy-pdf,请检查此处建议的解决方案。

https://github.com/nigma/django-easy-pdf

Template:

{% extends "easy_pdf/base.html" %}

{% block content %}
    <div id="content">
        <h1>Hi there!</h1>
    </div>
{% endblock %}

View:

from easy_pdf.views import PDFTemplateView

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"

If you want to use django-easy-pdf on Python 3 check the solution suggested here.


回答 2

我只是为CBV打了个招。未在生产中使用,但会为我生成PDF。可能需要为错误报告方面的事情工作,但到目前为止仍能解决问题。

import StringIO
from cgi import escape
from xhtml2pdf import pisa
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import TemplateView

class PDFTemplateResponse(TemplateResponse):

    def generate_pdf(self, retval):

        html = self.content

        result = StringIO.StringIO()
        rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

        if rendering.err:
            return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
        else:
            self.content = result.getvalue()

    def __init__(self, *args, **kwargs):
        super(PDFTemplateResponse, self).__init__(*args, mimetype='application/pdf', **kwargs)
        self.add_post_render_callback(self.generate_pdf)


class PDFTemplateView(TemplateView):
    response_class = PDFTemplateResponse

像这样使用:

class MyPdfView(PDFTemplateView):
    template_name = 'things/pdf.html'

I just whipped this up for CBV. Not used in production but generates a PDF for me. Probably needs work for the error reporting side of things but does the trick so far.

import StringIO
from cgi import escape
from xhtml2pdf import pisa
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import TemplateView

class PDFTemplateResponse(TemplateResponse):

    def generate_pdf(self, retval):

        html = self.content

        result = StringIO.StringIO()
        rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

        if rendering.err:
            return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
        else:
            self.content = result.getvalue()

    def __init__(self, *args, **kwargs):
        super(PDFTemplateResponse, self).__init__(*args, mimetype='application/pdf', **kwargs)
        self.add_post_render_callback(self.generate_pdf)


class PDFTemplateView(TemplateView):
    response_class = PDFTemplateResponse

Used like:

class MyPdfView(PDFTemplateView):
    template_name = 'things/pdf.html'

回答 3

使用以下包装之一尝试wkhtmltopdf

django-wkhtmltopdfpython-pdfkit

这对我来说非常有效,支持javascript和css或webkit浏览器支持的任何功能。

有关更多详细的教程,请参见此博客文章。

Try wkhtmltopdf with either one of the following wrappers

django-wkhtmltopdf or python-pdfkit

This worked great for me,supports javascript and css or anything for that matter which a webkit browser supports.

For more detailed tutorial please see this blog post


回答 4

在尝试使它工作了许多小时之后,我终于找到了这个:https : //github.com/vierno/django-xhtml2pdf

这是https://github.com/chrisglass/django-xhtml2pdf的一个分支,它为基于类的通用视图提供了mixin。我这样使用它:

    # views.py
    from django_xhtml2pdf.views import PdfMixin
    class GroupPDFGenerate(PdfMixin, DetailView):
        model = PeerGroupSignIn
        template_name = 'groups/pdf.html'

    # templates/groups/pdf.html
    <html>
    <style>
    @page { your xhtml2pdf pisa PDF parameters }
    </style>
    </head>
    <body>
        <div id="header_content"> (this is defined in the style section)
            <h1>{{ peergroupsignin.this_group_title }}</h1>
            ...

填充模板字段时,请使用您在视图中定义的所有小写字母的模型名称。由于它是GCBV,因此您可以在urls.py中将其称为“ .as_view”:

    # urls.py (using url namespaces defined in the main urls.py file)
    url(
        regex=r"^(?P<pk>\d+)/generate_pdf/$",
        view=views.GroupPDFGenerate.as_view(),
        name="generate_pdf",
       ),

After trying to get this to work for too many hours, I finally found this: https://github.com/vierno/django-xhtml2pdf

It’s a fork of https://github.com/chrisglass/django-xhtml2pdf that provides a mixin for a generic class-based view. I used it like this:

    # views.py
    from django_xhtml2pdf.views import PdfMixin
    class GroupPDFGenerate(PdfMixin, DetailView):
        model = PeerGroupSignIn
        template_name = 'groups/pdf.html'

    # templates/groups/pdf.html
    <html>
    <style>
    @page { your xhtml2pdf pisa PDF parameters }
    </style>
    </head>
    <body>
        <div id="header_content"> (this is defined in the style section)
            <h1>{{ peergroupsignin.this_group_title }}</h1>
            ...

Use the model name you defined in your view in all lowercase when populating the template fields. Because its a GCBV, you can just call it as ‘.as_view’ in your urls.py:

    # urls.py (using url namespaces defined in the main urls.py file)
    url(
        regex=r"^(?P<pk>\d+)/generate_pdf/$",
        view=views.GroupPDFGenerate.as_view(),
        name="generate_pdf",
       ),

回答 5

您可以使用iReport编辑器定义布局,并在jasper报表服务器中发布报表。发布后,您可以调用rest api以获取结果。

这是功能测试:

from django.test import TestCase
from x_reports_jasper.models import JasperServerClient

"""
    to try integraction with jasper server through rest
"""
class TestJasperServerClient(TestCase):

    # define required objects for tests
    def setUp(self):

        # load the connection to remote server
        try:

            self.j_url = "http://127.0.0.1:8080/jasperserver"
            self.j_user = "jasperadmin"
            self.j_pass = "jasperadmin"

            self.client = JasperServerClient.create_client(self.j_url,self.j_user,self.j_pass)

        except Exception, e:
            # if errors could not execute test given prerrequisites
            raise

    # test exception when server data is invalid
    def test_login_to_invalid_address_should_raise(self):
        self.assertRaises(Exception,JasperServerClient.create_client, "http://127.0.0.1:9090/jasperserver",self.j_user,self.j_pass)

    # test execute existent report in server
    def test_get_report(self):

        r_resource_path = "/reports/<PathToPublishedReport>"
        r_format = "pdf"
        r_params = {'PARAM_TO_REPORT':"1",}

        #resource_meta = client.load_resource_metadata( rep_resource_path )

        [uuid,out_mime,out_data] = self.client.generate_report(r_resource_path,r_format,r_params)
        self.assertIsNotNone(uuid)

这是调用实现的示例:

from django.db import models
import requests
import sys
from xml.etree import ElementTree
import logging 

# module logger definition
logger = logging.getLogger(__name__)

# Create your models here.
class JasperServerClient(models.Manager):

    def __handle_exception(self, exception_root, exception_id, exec_info ):
        type, value, traceback = exec_info
        raise JasperServerClientError(exception_root, exception_id), None, traceback

    # 01: REPORT-METADATA 
    #   get resource description to generate the report
    def __handle_report_metadata(self, rep_resourcepath):

        l_path_base_resource = "/rest/resource"
        l_path = self.j_url + l_path_base_resource
        logger.info( "metadata (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        resource_response = None
        try:
            resource_response = requests.get( "%s%s" %( l_path ,rep_resourcepath) , cookies = self.login_response.cookies)

        except Exception, e:
            self.__handle_exception(e, "REPORT_METADATA:CALL_ERROR", sys.exc_info())

        resource_response_dom = None
        try:
            # parse to dom and set parameters
            logger.debug( " - response [data=%s]"  %( resource_response.text) )
            resource_response_dom = ElementTree.fromstring(resource_response.text)

            datum = "" 
            for node in resource_response_dom.getiterator():
                datum = "%s<br />%s - %s" % (datum, node.tag, node.text)
            logger.debug( " - response [xml=%s]"  %( datum ) )

            #
            self.resource_response_payload= resource_response.text
            logger.info( "metadata (end) ")
        except Exception, e:
            logger.error( "metadata (error) [%s]" % (e))
            self.__handle_exception(e, "REPORT_METADATA:PARSE_ERROR", sys.exc_info())


    # 02: REPORT-PARAMS 
    def __add_report_params(self, metadata_text, params ):
        if(type(params) != dict):
            raise TypeError("Invalid parameters to report")
        else:
            logger.info( "add-params (begin) []" )
            #copy parameters
            l_params = {}
            for k,v in params.items():
                l_params[k]=v
            # get the payload metadata
            metadata_dom = ElementTree.fromstring(metadata_text)
            # add attributes to payload metadata
            root = metadata_dom #('report'):

            for k,v in l_params.items():
                param_dom_element = ElementTree.Element('parameter')
                param_dom_element.attrib["name"] = k
                param_dom_element.text = v
                root.append(param_dom_element)

            #
            metadata_modified_text =ElementTree.tostring(metadata_dom, encoding='utf8', method='xml')
            logger.info( "add-params (end) [payload-xml=%s]" %( metadata_modified_text )  )
            return metadata_modified_text



    # 03: REPORT-REQUEST-CALL 
    #   call to generate the report
    def __handle_report_request(self, rep_resourcepath, rep_format, rep_params):

        # add parameters
        self.resource_response_payload = self.__add_report_params(self.resource_response_payload,rep_params)

        # send report request

        l_path_base_genreport = "/rest/report"
        l_path = self.j_url + l_path_base_genreport
        logger.info( "report-request (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        genreport_response = None
        try:
            genreport_response = requests.put( "%s%s?RUN_OUTPUT_FORMAT=%s" %(l_path,rep_resourcepath,rep_format),data=self.resource_response_payload, cookies = self.login_response.cookies )
            logger.info( " - send-operation-result [value=%s]"  %( genreport_response.text) )
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:CALL_ERROR", sys.exc_info())


        # parse the uuid of the requested report
        genreport_response_dom = None

        try:
            genreport_response_dom = ElementTree.fromstring(genreport_response.text)

            for node in genreport_response_dom.findall("uuid"):
                datum = "%s" % (node.text)

            genreport_uuid = datum      

            for node in genreport_response_dom.findall("file/[@type]"):
                datum = "%s" % (node.text)
            genreport_mime = datum

            logger.info( "report-request (end) [uuid=%s,mime=%s]"  %( genreport_uuid, genreport_mime) )

            return [genreport_uuid,genreport_mime]
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:PARSE_ERROR", sys.exc_info())

    # 04: REPORT-RETRIEVE RESULTS 
    def __handle_report_reply(self, genreport_uuid ):


        l_path_base_getresult = "/rest/report"
        l_path = self.j_url + l_path_base_getresult 
        logger.info( "report-reply (begin) [uuid=%s,path=%s]"  %( genreport_uuid,l_path) )

        getresult_response = requests.get( "%s%s/%s?file=report" %(self.j_url,l_path_base_getresult,genreport_uuid),data=self.resource_response_payload, cookies = self.login_response.cookies )
        l_result_header_mime =getresult_response.headers['Content-Type']

        logger.info( "report-reply (end) [uuid=%s,mime=%s]"  %( genreport_uuid, l_result_header_mime) )
        return [l_result_header_mime, getresult_response.content]

    # public methods ---------------------------------------    

    # tries the authentication with jasperserver throug rest
    def login(self, j_url, j_user,j_pass):
        self.j_url= j_url

        l_path_base_auth = "/rest/login"
        l_path = self.j_url + l_path_base_auth

        logger.info( "login (begin) [path=%s]"  %( l_path) )

        try:
            self.login_response = requests.post(l_path , params = {
                    'j_username':j_user,
                    'j_password':j_pass
                })                  

            if( requests.codes.ok != self.login_response.status_code ):
                self.login_response.raise_for_status()

            logger.info( "login (end)" )
            return True
            # see http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/

        except Exception, e:
            logger.error("login (error) [e=%s]" % e )
            self.__handle_exception(e, "LOGIN:CALL_ERROR",sys.exc_info())
            #raise

    def generate_report(self, rep_resourcepath,rep_format,rep_params):
        self.__handle_report_metadata(rep_resourcepath)
        [uuid,mime] = self.__handle_report_request(rep_resourcepath, rep_format,rep_params)
        # TODO: how to handle async?
        [out_mime,out_data] = self.__handle_report_reply(uuid)
        return [uuid,out_mime,out_data]

    @staticmethod
    def create_client(j_url, j_user, j_pass):
        client = JasperServerClient()
        login_res = client.login( j_url, j_user, j_pass )
        return client


class JasperServerClientError(Exception):

    def __init__(self,exception_root,reason_id,reason_message=None):
        super(JasperServerClientError, self).__init__(str(reason_message))
        self.code = reason_id 
        self.description = str(exception_root) + " " + str(reason_message)
    def __str__(self):
        return self.code + " " + self.description

You can use iReport editor to define the layout, and publish the report in jasper reports server. After publish you can invoke the rest api to get the results.

Here is the test of the functionality:

from django.test import TestCase
from x_reports_jasper.models import JasperServerClient

"""
    to try integraction with jasper server through rest
"""
class TestJasperServerClient(TestCase):

    # define required objects for tests
    def setUp(self):

        # load the connection to remote server
        try:

            self.j_url = "http://127.0.0.1:8080/jasperserver"
            self.j_user = "jasperadmin"
            self.j_pass = "jasperadmin"

            self.client = JasperServerClient.create_client(self.j_url,self.j_user,self.j_pass)

        except Exception, e:
            # if errors could not execute test given prerrequisites
            raise

    # test exception when server data is invalid
    def test_login_to_invalid_address_should_raise(self):
        self.assertRaises(Exception,JasperServerClient.create_client, "http://127.0.0.1:9090/jasperserver",self.j_user,self.j_pass)

    # test execute existent report in server
    def test_get_report(self):

        r_resource_path = "/reports/<PathToPublishedReport>"
        r_format = "pdf"
        r_params = {'PARAM_TO_REPORT':"1",}

        #resource_meta = client.load_resource_metadata( rep_resource_path )

        [uuid,out_mime,out_data] = self.client.generate_report(r_resource_path,r_format,r_params)
        self.assertIsNotNone(uuid)

And here is an example of the invocation implementation:

from django.db import models
import requests
import sys
from xml.etree import ElementTree
import logging 

# module logger definition
logger = logging.getLogger(__name__)

# Create your models here.
class JasperServerClient(models.Manager):

    def __handle_exception(self, exception_root, exception_id, exec_info ):
        type, value, traceback = exec_info
        raise JasperServerClientError(exception_root, exception_id), None, traceback

    # 01: REPORT-METADATA 
    #   get resource description to generate the report
    def __handle_report_metadata(self, rep_resourcepath):

        l_path_base_resource = "/rest/resource"
        l_path = self.j_url + l_path_base_resource
        logger.info( "metadata (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        resource_response = None
        try:
            resource_response = requests.get( "%s%s" %( l_path ,rep_resourcepath) , cookies = self.login_response.cookies)

        except Exception, e:
            self.__handle_exception(e, "REPORT_METADATA:CALL_ERROR", sys.exc_info())

        resource_response_dom = None
        try:
            # parse to dom and set parameters
            logger.debug( " - response [data=%s]"  %( resource_response.text) )
            resource_response_dom = ElementTree.fromstring(resource_response.text)

            datum = "" 
            for node in resource_response_dom.getiterator():
                datum = "%s<br />%s - %s" % (datum, node.tag, node.text)
            logger.debug( " - response [xml=%s]"  %( datum ) )

            #
            self.resource_response_payload= resource_response.text
            logger.info( "metadata (end) ")
        except Exception, e:
            logger.error( "metadata (error) [%s]" % (e))
            self.__handle_exception(e, "REPORT_METADATA:PARSE_ERROR", sys.exc_info())


    # 02: REPORT-PARAMS 
    def __add_report_params(self, metadata_text, params ):
        if(type(params) != dict):
            raise TypeError("Invalid parameters to report")
        else:
            logger.info( "add-params (begin) []" )
            #copy parameters
            l_params = {}
            for k,v in params.items():
                l_params[k]=v
            # get the payload metadata
            metadata_dom = ElementTree.fromstring(metadata_text)
            # add attributes to payload metadata
            root = metadata_dom #('report'):

            for k,v in l_params.items():
                param_dom_element = ElementTree.Element('parameter')
                param_dom_element.attrib["name"] = k
                param_dom_element.text = v
                root.append(param_dom_element)

            #
            metadata_modified_text =ElementTree.tostring(metadata_dom, encoding='utf8', method='xml')
            logger.info( "add-params (end) [payload-xml=%s]" %( metadata_modified_text )  )
            return metadata_modified_text



    # 03: REPORT-REQUEST-CALL 
    #   call to generate the report
    def __handle_report_request(self, rep_resourcepath, rep_format, rep_params):

        # add parameters
        self.resource_response_payload = self.__add_report_params(self.resource_response_payload,rep_params)

        # send report request

        l_path_base_genreport = "/rest/report"
        l_path = self.j_url + l_path_base_genreport
        logger.info( "report-request (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        genreport_response = None
        try:
            genreport_response = requests.put( "%s%s?RUN_OUTPUT_FORMAT=%s" %(l_path,rep_resourcepath,rep_format),data=self.resource_response_payload, cookies = self.login_response.cookies )
            logger.info( " - send-operation-result [value=%s]"  %( genreport_response.text) )
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:CALL_ERROR", sys.exc_info())


        # parse the uuid of the requested report
        genreport_response_dom = None

        try:
            genreport_response_dom = ElementTree.fromstring(genreport_response.text)

            for node in genreport_response_dom.findall("uuid"):
                datum = "%s" % (node.text)

            genreport_uuid = datum      

            for node in genreport_response_dom.findall("file/[@type]"):
                datum = "%s" % (node.text)
            genreport_mime = datum

            logger.info( "report-request (end) [uuid=%s,mime=%s]"  %( genreport_uuid, genreport_mime) )

            return [genreport_uuid,genreport_mime]
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:PARSE_ERROR", sys.exc_info())

    # 04: REPORT-RETRIEVE RESULTS 
    def __handle_report_reply(self, genreport_uuid ):


        l_path_base_getresult = "/rest/report"
        l_path = self.j_url + l_path_base_getresult 
        logger.info( "report-reply (begin) [uuid=%s,path=%s]"  %( genreport_uuid,l_path) )

        getresult_response = requests.get( "%s%s/%s?file=report" %(self.j_url,l_path_base_getresult,genreport_uuid),data=self.resource_response_payload, cookies = self.login_response.cookies )
        l_result_header_mime =getresult_response.headers['Content-Type']

        logger.info( "report-reply (end) [uuid=%s,mime=%s]"  %( genreport_uuid, l_result_header_mime) )
        return [l_result_header_mime, getresult_response.content]

    # public methods ---------------------------------------    

    # tries the authentication with jasperserver throug rest
    def login(self, j_url, j_user,j_pass):
        self.j_url= j_url

        l_path_base_auth = "/rest/login"
        l_path = self.j_url + l_path_base_auth

        logger.info( "login (begin) [path=%s]"  %( l_path) )

        try:
            self.login_response = requests.post(l_path , params = {
                    'j_username':j_user,
                    'j_password':j_pass
                })                  

            if( requests.codes.ok != self.login_response.status_code ):
                self.login_response.raise_for_status()

            logger.info( "login (end)" )
            return True
            # see http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/

        except Exception, e:
            logger.error("login (error) [e=%s]" % e )
            self.__handle_exception(e, "LOGIN:CALL_ERROR",sys.exc_info())
            #raise

    def generate_report(self, rep_resourcepath,rep_format,rep_params):
        self.__handle_report_metadata(rep_resourcepath)
        [uuid,mime] = self.__handle_report_request(rep_resourcepath, rep_format,rep_params)
        # TODO: how to handle async?
        [out_mime,out_data] = self.__handle_report_reply(uuid)
        return [uuid,out_mime,out_data]

    @staticmethod
    def create_client(j_url, j_user, j_pass):
        client = JasperServerClient()
        login_res = client.login( j_url, j_user, j_pass )
        return client


class JasperServerClientError(Exception):

    def __init__(self,exception_root,reason_id,reason_message=None):
        super(JasperServerClientError, self).__init__(str(reason_message))
        self.code = reason_id 
        self.description = str(exception_root) + " " + str(reason_message)
    def __str__(self):
        return self.code + " " + self.description

回答 6

我得到了从html模板生成PDF的代码:

    import os

    from weasyprint import HTML

    from django.template import Template, Context
    from django.http import HttpResponse 


    def generate_pdf(self, report_id):

            # Render HTML into memory and get the template firstly
            template_file_loc = os.path.join(os.path.dirname(__file__), os.pardir, 'templates', 'the_template_pdf_generator.html')
            template_contents = read_all_as_str(template_file_loc)
            render_template = Template(template_contents)

            #rendering_map is the dict for params in the template 
            render_definition = Context(rendering_map)
            render_output = render_template.render(render_definition)

            # Using Rendered HTML to generate PDF
            response = HttpResponse(content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=%s-%s-%s.pdf' % \
                                              ('topic-test','topic-test', '2018-05-04')
            # Generate PDF
            pdf_doc = HTML(string=render_output).render()
            pdf_doc.pages[0].height = pdf_doc.pages[0]._page_box.children[0].children[
                0].height  # Make PDF file as single page file 
            pdf_doc.write_pdf(response)
            return response

    def read_all_as_str(self, file_loc, read_method='r'):
        if file_exists(file_loc):
            handler = open(file_loc, read_method)
            contents = handler.read()
            handler.close()
            return contents
        else:
            return 'file not exist'  

I get the code to generate the PDF from html template :

    import os

    from weasyprint import HTML

    from django.template import Template, Context
    from django.http import HttpResponse 


    def generate_pdf(self, report_id):

            # Render HTML into memory and get the template firstly
            template_file_loc = os.path.join(os.path.dirname(__file__), os.pardir, 'templates', 'the_template_pdf_generator.html')
            template_contents = read_all_as_str(template_file_loc)
            render_template = Template(template_contents)

            #rendering_map is the dict for params in the template 
            render_definition = Context(rendering_map)
            render_output = render_template.render(render_definition)

            # Using Rendered HTML to generate PDF
            response = HttpResponse(content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=%s-%s-%s.pdf' % \
                                              ('topic-test','topic-test', '2018-05-04')
            # Generate PDF
            pdf_doc = HTML(string=render_output).render()
            pdf_doc.pages[0].height = pdf_doc.pages[0]._page_box.children[0].children[
                0].height  # Make PDF file as single page file 
            pdf_doc.write_pdf(response)
            return response

    def read_all_as_str(self, file_loc, read_method='r'):
        if file_exists(file_loc):
            handler = open(file_loc, read_method)
            contents = handler.read()
            handler.close()
            return contents
        else:
            return 'file not exist'  

回答 7

如果您的html模板中包含上下文数据以及css和js。比使用pdfjs的选择更好

在您的代码中,您可以像这样使用。

from django.template.loader import get_template
import pdfkit
from django.conf import settings

context={....}
template = get_template('reports/products.html')
html_string = template.render(context)
pdfkit.from_string(html_string, os.path.join(settings.BASE_DIR, "media", 'products_report-%s.pdf'%(id)))

在您的HTML中,您可以链接外部或内部的CSS和js,它将生成最佳质量的pdf。

If you have context data along with css and js in your html template. Than you have good option to use pdfjs.

In your code you can use like this.

from django.template.loader import get_template
import pdfkit
from django.conf import settings

context={....}
template = get_template('reports/products.html')
html_string = template.render(context)
pdfkit.from_string(html_string, os.path.join(settings.BASE_DIR, "media", 'products_report-%s.pdf'%(id)))

In your HTML you can link extranal or internal css and js, it will generate best quality of pdf.


如何从Django 1.7的初始迁移迁移回去?

问题:如何从Django 1.7的初始迁移迁移回去?

我用一些模型创建了一个新的应用程序,现在我发现一些模型没有经过深思熟虑。由于我尚未提交代码,因此明智的做法是将数据库迁移到最后的良好状态,并使用更好的模型重新进行迁移。在这种情况下,最后的良好状态是新应用程序不存在的数据库。

如何从Django 1.7的初始迁移迁移回去?

South一个可以这样做:

python manage.py migrate <app> zero

<app>将从迁移历史记录中清除并删除的所有表<app>

如何在Django 1.7迁移中做到这一点?

I created a new app with some models and now I noticed that some of the models are poorly thought out. As I haven’t committed the code the sensible thing would be to migrate the database to last good state and redo the migration with better models. In this case the last good state is database where the new app doesn’t exist.

How can I migrate back from initial migration in Django 1.7?

In South one could do:

python manage.py migrate <app> zero

Which would clear <app> from migration history and drop all tables of <app>.

How to do this with Django 1.7 migrations?


回答 0

您也可以使用Django 1.7+进行相同操作:

python manage.py migrate <app> zero

<app>将从迁移历史记录中清除,并删除所有的表<app>

有关更多信息,请参见django文档

You can do the same with Django 1.7+ also:

python manage.py migrate <app> zero

This clears <app> from migration history and drops all tables of <app>

See django docs for more info.


回答 1

您还可以使用版本号:

python manage.py migrate <app> 0002

来源:https : //docs.djangoproject.com/en/1.7/ref/django-admin/#django-admin-migrate

you can also use the version number:

python manage.py migrate <app> 0002

Source: https://docs.djangoproject.com/en/1.7/ref/django-admin/#django-admin-migrate


使用Django在两个日期之间选择

问题:使用Django在两个日期之间选择

我正在寻找一个在Django日期之间进行选择的查询。

我知道如何使用原始SQL轻松做到这一点,但是如何使用Django ORM做到这一点呢?

这是我要在查询中添加30天之间的日期的地方:

start_date = datetime.datetime.now() + datetime.timedelta(-30)
context[self.varname] = self.model._default_manager.filter(
    current_issue__isnull=True
    ).live().order_by('-created_at')

I am looking to make a query that selects between dates with Django.

I know how to do this with raw SQL pretty easily, but how could this be achieved using the Django ORM?

This is where I want to add the between dates of 30 days in my query:

start_date = datetime.datetime.now() + datetime.timedelta(-30)
context[self.varname] = self.model._default_manager.filter(
    current_issue__isnull=True
    ).live().order_by('-created_at')

回答 0

使用__range运算符:

...filter(current_issue__isnull=True, created_at__range=(start_date, end_date))

Use the __range operator:

...filter(current_issue__isnull=True, created_at__range=(start_date, end_date))

回答 1


回答 2

两种方法

.filter(created_at__range=[from_date, to_date])

另一种方法

.filter(Q(created_at__gte=from_date)&Q(created_at__lte=to_date))
  • gte意味着大于等于
  • 小于等于

two methods

.filter(created_at__range=[from_date, to_date])

another method

.filter(Q(created_at__gte=from_date)&Q(created_at__lte=to_date))
  • gte means greater than equal
  • lte means less than equal

回答 3

如果您使用DateTimeField,则使用日期进行过滤将不包括最后一天的项目。

您需要将值转换为日期:

...filter(created_at__date__range=(start_date, end_date))

If you are using a DateTimeField, Filtering with dates won’t include items on the last day.

You need to casts the value as date:

...filter(created_at__date__range=(start_date, end_date))

TemplateDoesNotExist-Django错误

问题:TemplateDoesNotExist-Django错误

我正在使用Django Rest Framework。而且我不断出错

Exception Type: TemplateDoesNotExist
Exception Value: rest_framework/api.html

我不知道我怎么了。这是我第一次尝试使用REST Framework。这是代码。

views.py

import socket, json
from modules.data.models import *
from modules.utils import *
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from modules.actions.serializers import ActionSerializer


@api_view(['POST'])
@check_field_exists_wrapper("installation")
def api_actions(request, format = None):

    action_type = request.POST['action_type']
    if action_type == "Shutdown" : 
        send_message = '1'
        print "Shutting Down the system..."
    elif action_type == "Enable" : 
        send_message = '1'
        print "Enabling the system..."
    elif action_type == "Disable" : 
        send_message = '1'
        print "Disabling the system..."
    elif action_type == "Restart" : 
        send_message = '1'
        print "Restarting the system..."

    if action_type in ["Shutdown", "Enable", "Disable"] : PORT = 6000
    else : PORT = 6100

    controllers_list = Controller.objects.filter(installation_id = kwargs['installation_id'])

    for controller_obj in controllers_list:
        ip = controller_obj.ip
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((ip, PORT))
            s.send(send_message)
            s.close()
        except Exception as e:
            print("Exception when sending " + action_type +" command: "+str(e))

    return Response(status = status.HTTP_200_OK)

models.py

class Controller(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 255, unique = True)
    ip = models.CharField(max_length = 255, unique = True)
    installation_id = models.ForeignKey('Installation')

serializers.py

从django.forms中导入从rest_framework中导入小部件从modules.data.models中导入序列化器*

class ActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Controller
        fields = ('id', 'name', 'ip', 'installation_id')

urls.py

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns

urlpatterns = patterns('modules.actions.views',
    url(r'^$','api_actions',name='api_actions'),
)

I’m using Django Rest Framework. and I keep getting an error

Exception Type: TemplateDoesNotExist
Exception Value: rest_framework/api.html

I dont know how I’m going wrong. This is the first time I’m trying out hands on REST Framework. This is code.

views.py

import socket, json
from modules.data.models import *
from modules.utils import *
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from modules.actions.serializers import ActionSerializer


@api_view(['POST'])
@check_field_exists_wrapper("installation")
def api_actions(request, format = None):

    action_type = request.POST['action_type']
    if action_type == "Shutdown" : 
        send_message = '1'
        print "Shutting Down the system..."
    elif action_type == "Enable" : 
        send_message = '1'
        print "Enabling the system..."
    elif action_type == "Disable" : 
        send_message = '1'
        print "Disabling the system..."
    elif action_type == "Restart" : 
        send_message = '1'
        print "Restarting the system..."

    if action_type in ["Shutdown", "Enable", "Disable"] : PORT = 6000
    else : PORT = 6100

    controllers_list = Controller.objects.filter(installation_id = kwargs['installation_id'])

    for controller_obj in controllers_list:
        ip = controller_obj.ip
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((ip, PORT))
            s.send(send_message)
            s.close()
        except Exception as e:
            print("Exception when sending " + action_type +" command: "+str(e))

    return Response(status = status.HTTP_200_OK)

models.py

class Controller(models.Model):
    id = models.IntegerField(primary_key = True)
    name = models.CharField(max_length = 255, unique = True)
    ip = models.CharField(max_length = 255, unique = True)
    installation_id = models.ForeignKey('Installation')

serializers.py

from django.forms import widgets from rest_framework import serializers from modules.data.models import *

class ActionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Controller
        fields = ('id', 'name', 'ip', 'installation_id')

urls.py

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns

urlpatterns = patterns('modules.actions.views',
    url(r'^$','api_actions',name='api_actions'),
)

回答 0

确保您已rest_framework在中列出settings.py INSTALLED_APPS

Make sure you have rest_framework listed in your settings.py INSTALLED_APPS.


回答 1

对我来说,rest_framework/api.html由于安装损坏或其他未知原因,文件系统上实际上缺少该文件系统。重新安装djangorestframework解决了问题:

$ pip install --upgrade djangorestframework

For me, rest_framework/api.html was actually missing on the filesystem due to a corrupt installation or some other unknown reason. Reinstalling djangorestframework fixed the problem:

$ pip install --upgrade djangorestframework

回答 2

请注意,DRF尝试以请求的相同格式返回数据。在您的浏览器中,这很可能是HTML。要指定替代响应,请使用?format=参数。例如:?format=json

如其他受访者所述,TemplateDoesNotExist当您在浏览器中访问API端点并且未将rest_framework包含在已安装应用程序列表中时,通常会发生此错误。

如果您的应用程序列表中未包含DRF,但又不想使用HTML Admin DRF页面,请尝试使用其他格式来“避免”此错误消息。

来自此处的文档的更多信息:http : //www.django-rest-framework.org/topics/browsable-api/#formats

Please note that the DRF attempts to return data in the same format that was requested. From your browser, this is most likely HTML. To specify an alternative response, use the ?format= parameter. For example: ?format=json.

The TemplateDoesNotExist error occurs most commonly when you are visiting an API endpoint in your browser and you do not have the rest_framework included in your list of installed apps, as described by other respondents.

If you do not have DRF included in your list of apps, but don’t want to use the HTML Admin DRF page, try using an alternative format to ‘side-step’ this error message.

More info from the docs here: http://www.django-rest-framework.org/topics/browsable-api/#formats


回答 3

不是您的情况,而是为定制loaders了可能的原因Django。例如,如果您有设置(自以来Django 1.8):

TEMPLATES = [
{
    ...
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages'
        ],
        'loaders': [
            'django.template.loaders.filesystem.Loader',
        ],
        ...
    }
}]

Django将不尝试一下使用模板的应用程序文件夹,因为你应该明确地添加django.template.loaders.app_directories.Loaderloaders了这一点。

请注意,默认情况下django.template.loaders.app_directories.Loader包含在中loaders

Not your case, but also possible reason is customized loaders for Django. For example, if you have in settings (since Django 1.8):

TEMPLATES = [
{
    ...
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages'
        ],
        'loaders': [
            'django.template.loaders.filesystem.Loader',
        ],
        ...
    }
}]

Django will not try to look at applications folders with templates, because you should explicitly add django.template.loaders.app_directories.Loader into loaders for that.

Notice, that by default django.template.loaders.app_directories.Loader included into loaders.


回答 4

我遇到了同样的错误消息。就我而言,这是由于将后端设置为Jinja2。在我的设置文件中:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.jinja2.Jinja2',
...

将其更改回默认值可解决此问题:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
...

仍然不确定是否有办法将Jinja2后端与rest_framework一起使用。

I ran into the same error message. In my case, it was due to setting the backend to Jinja2. In my settings file:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.jinja2.Jinja2',
...

Changing this back to the default fixed the problem:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
...

Still not sure if there is a way to use the Jinja2 backend with rest_framework.


“无法解包的值太多”异常

问题:“无法解包的值太多”异常

我正在Django中进行项目开发,我刚刚开始尝试扩展User模型以创建用户个人资料。

不幸的是,我遇到一个问题:每次尝试在模板(user.get_template.lastIP例如)中获取用户的个人资料时,都会出现以下错误:

环境:

请求方法:GET
要求网址:http:// localhost:8000 /
Django版本:1.1
Python版本:2.6.1

模板错误:
在模板/path/to/base.tpl中,第19行出现错误
   渲染时遇到异常:太多值无法解压

19:您好,{{user.username}}({{user.get_profile.rep}})。近况如何?登出


异常类型:/处的TemplateSyntaxError
异常值:渲染时捕获到异常:太多值无法解包

关于发生了什么或我做错了什么的任何想法?

I’m working on a project in Django and I’ve just started trying to extend the User model in order to make user profiles.

Unfortunately, I’ve run into a problem: Every time I try to get the user’s profile inside of a template (user.get_template.lastIP, for example), I get the following error:

Environment:

Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.1
Python Version: 2.6.1

Template error:
In template /path/to/base.tpl, error at line 19
   Caught an exception while rendering: too many values to unpack

19 :                Hello, {{user.username}} ({{ user.get_profile.rep}}). How's it goin? Logout


Exception Type: TemplateSyntaxError at /
Exception Value: Caught an exception while rendering: too many values to unpack

Any ideas as to what’s going on or what I’m doing wrong?


回答 0

该异常意味着您正试图解压缩一个元组,但是相对于目标变量的数量,该元组的值太多。例如:这项工作,先打印1,然后打印2,然后打印3

def returnATupleWithThreeValues():
    return (1,2,3)
a,b,c = returnATupleWithThreeValues()
print a
print b
print c

但这会引发您的错误

def returnATupleWithThreeValues():
    return (1,2,3)
a,b = returnATupleWithThreeValues()
print a
print b

加薪

Traceback (most recent call last):
  File "c.py", line 3, in ?
    a,b = returnATupleWithThreeValues()
ValueError: too many values to unpack

现在,我不知道在您的情况下发生这种情况的原因,但也许此答案将为您指明正确的方向。

That exception means that you are trying to unpack a tuple, but the tuple has too many values with respect to the number of target variables. For example: this work, and prints 1, then 2, then 3

def returnATupleWithThreeValues():
    return (1,2,3)
a,b,c = returnATupleWithThreeValues()
print a
print b
print c

But this raises your error

def returnATupleWithThreeValues():
    return (1,2,3)
a,b = returnATupleWithThreeValues()
print a
print b

raises

Traceback (most recent call last):
  File "c.py", line 3, in ?
    a,b = returnATupleWithThreeValues()
ValueError: too many values to unpack

Now, the reason why this happens in your case, I don’t know, but maybe this answer will point you in the right direction.


回答 1

尝试解压缩一个变量,

python会将其作为列表处理,

然后从清单中打开包装

def returnATupleWithThreeValues():
    return (1,2,3)
a = returnATupleWithThreeValues() # a is a list (1,2,3)
print a[0] # list[0] = 1
print a[1] # list[1] = 2
print a[2] # list[2] = 3

try unpacking in one variable,

python will handle it as a list,

then unpack from the list

def returnATupleWithThreeValues():
    return (1,2,3)
a = returnATupleWithThreeValues() # a is a list (1,2,3)
print a[0] # list[0] = 1
print a[1] # list[1] = 2
print a[2] # list[2] = 3

回答 2

这个问题看起来很熟悉,所以我想我可以看看能否从有限的信息中进行复制。

快速搜索在James Bennett的博客中找到了一个条目,其中提到在使用UserProfile扩展User模型时,settings.py中的常见错误可能导致Django抛出此错误。

引用博客条目:

该设置的值不是“ appname.models.modelname”,而只是“ appname.modelname”。原因是Django没有使用它进行直接导入。相反,它使用内部模型加载功能,该功能仅需要应用程序的名称和模型的名称。尝试在AUTH_PROFILE_MODULE设置中执行“ appname.models.modelname”或“ projectname.appname.models.modelname”之类的操作会导致Django崩溃,并出现可怕的“太多值无法解包”错误,因此请确保您已经将“ appname.modelname”放在AUTH_PROFILE_MODULE的值中,不要输入其他任何内容。

如果OP复制了更多的回溯,我希望看到类似下面的内容,可以通过在我的AUTH_PROFILE_MODULE设置中添加“模型”来进行复制。

TemplateSyntaxError at /

Caught an exception while rendering: too many values to unpack

Original Traceback (most recent call last):
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 535, in resolve
    obj = self.var.resolve(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 676, in resolve
    value = self._resolve_lookup(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 711, in _resolve_lookup
    current = current()
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/contrib/auth/models.py", line 291, in get_profile
    app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
ValueError: too many values to unpack

我认为这是Django仍然具有一些导入魔术的少数情况之一,当小错误未引发预期的异常时,该魔术往往会引起混乱。

您可以在回溯结束时看到,我发布了如何对AUTH_PROFILE_MODULE使用除形式为“ appname.modelname”以外的任何内容会导致“ app_label,model_name = settings.AUTH_PROFILE_MODULE.split(’。’)”这一行引发“无法解包的值太多”错误。

我99%确信这是这里遇到的原始问题。

This problem looked familiar so I thought I’d see if I could replicate from the limited amount of information.

A quick search turned up an entry in James Bennett’s blog here which mentions that when working with the UserProfile to extend the User model a common mistake in settings.py can cause Django to throw this error.

To quote the blog entry:

The value of the setting is not “appname.models.modelname”, it’s just “appname.modelname”. The reason is that Django is not using this to do a direct import; instead, it’s using an internal model-loading function which only wants the name of the app and the name of the model. Trying to do things like “appname.models.modelname” or “projectname.appname.models.modelname” in the AUTH_PROFILE_MODULE setting will cause Django to blow up with the dreaded “too many values to unpack” error, so make sure you’ve put “appname.modelname”, and nothing else, in the value of AUTH_PROFILE_MODULE.

If the OP had copied more of the traceback I would expect to see something like the one below which I was able to duplicate by adding “models” to my AUTH_PROFILE_MODULE setting.

TemplateSyntaxError at /

Caught an exception while rendering: too many values to unpack

Original Traceback (most recent call last):
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/debug.py", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 535, in resolve
    obj = self.var.resolve(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 676, in resolve
    value = self._resolve_lookup(context)
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/template/__init__.py", line 711, in _resolve_lookup
    current = current()
  File "/home/brandon/Development/DJANGO_VERSIONS/Django-1.0/django/contrib/auth/models.py", line 291, in get_profile
    app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
ValueError: too many values to unpack

This I think is one of the few cases where Django still has a bit of import magic that tends to cause confusion when a small error doesn’t throw the expected exception.

You can see at the end of the traceback that I posted how using anything other than the form “appname.modelname” for the AUTH_PROFILE_MODULE would cause the line “app_label, model_name = settings.AUTH_PROFILE_MODULE.split(‘.’)” to throw the “too many values to unpack” error.

I’m 99% sure that this was the original problem encountered here.


回答 3

get_profile()调用中某处很可能有错误。在您看来,在返回请求对象之前,请放置以下行:

request.user.get_profile()

它会引发错误,并为您提供更详细的追溯,然后您可以将其用于进一步的调试。

Most likely there is an error somewhere in the get_profile() call. In your view, before you return the request object, put this line:

request.user.get_profile()

It should raise the error, and give you a more detailed traceback, which you can then use to further debug.


回答 4

当我使用Jinja2作为模板时,这会发生在我身上。通过使用django_extensions中runserver_plus命令运行开发服务器可以解决此问题。

它使用werkzeug调试器,该调试器也要好得多,并且具有非常好的交互式调试控制台。在任何帧(在调用堆栈中)启动python shell时,它具有ajax的魔力,因此您可以进行调试。

This happens to me when I’m using Jinja2 for templates. The problem can be solved by running the development server using the runserver_plus command from django_extensions.

It uses the werkzeug debugger which also happens to be a lot better and has a very nice interactive debugging console. It does some ajax magic to launch a python shell at any frame (in the call stack) so you can debug.


创建Django模型或更新(如果存在)

问题:创建Django模型或更新(如果存在)

我想创建一个模型对象,例如Person,如果不存在person的id,或者我将得到该person对象。

创建新人员的代码如下:

class Person(models.Model):
    identifier = models.CharField(max_length = 10)
    name = models.CharField(max_length = 20)
    objects = PersonManager()

class PersonManager(models.Manager):
    def create_person(self, identifier):
        person = self.create(identifier = identifier)
        return person

但是我不知道在哪里检查并获取现有的人对象。

I want to create a model object, like Person, if person’s id doesn’t not exist, or I will get that person object.

The code to create a new person as following:

class Person(models.Model):
    identifier = models.CharField(max_length = 10)
    name = models.CharField(max_length = 20)
    objects = PersonManager()

class PersonManager(models.Manager):
    def create_person(self, identifier):
        person = self.create(identifier = identifier)
        return person

But I don’t know where to check and get the existing person object.


回答 0

如果您正在寻找“如果存在则更新,否则会创建”用例,请参阅@Zags绝佳答案


Django中已经有一个get_or_createhttps://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

对你来说可能是:

id = 'some identifier'
person, created = Person.objects.get_or_create(identifier=id)

if created:
   # means you have created a new person
else:
   # person just refers to the existing one

If you’re looking for “update if exists else create” use case, please refer to @Zags excellent answer


Django already has a get_or_create, https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

For you it could be :

id = 'some identifier'
person, created = Person.objects.get_or_create(identifier=id)

if created:
   # means you have created a new person
else:
   # person just refers to the existing one

回答 1

目前尚不清楚您的问题是否要求使用get_or_create方法(至少可从Django 1.3获得)或update_or_create方法(Django 1.7中的新增功能)。这取决于您要如何更新用户对象。

用法示例如下:

# In both cases, the call will get a person object with matching
# identifier or create one if none exists; if a person is created,
# it will be created with name equal to the value in `name`.

# In this case, if the Person already exists, its existing name is preserved
person, created = Person.objects.get_or_create(
        identifier=identifier, defaults={"name": name}
)

# In this case, if the Person already exists, its name is updated
person, created = Person.objects.update_or_create(
        identifier=identifier, defaults={"name": name}
)

It’s unclear whether your question is asking for the get_or_create method (available from at least Django 1.3) or the update_or_create method (new in Django 1.7). It depends on how you want to update the user object.

Sample use is as follows:

# In both cases, the call will get a person object with matching
# identifier or create one if none exists; if a person is created,
# it will be created with name equal to the value in `name`.

# In this case, if the Person already exists, its existing name is preserved
person, created = Person.objects.get_or_create(
        identifier=identifier, defaults={"name": name}
)

# In this case, if the Person already exists, its name is updated
person, created = Person.objects.update_or_create(
        identifier=identifier, defaults={"name": name}
)

回答 2

Django支持此功能,请检查get_or_create

person, created = Person.objects.get_or_create(name='abc')
if created:
    # A new person object created
else:
    # person object already exists

Django has support for this, check get_or_create

person, created = Person.objects.get_or_create(name='abc')
if created:
    # A new person object created
else:
    # person object already exists

回答 3

对于仅少量的对象,update_or_create可以很好地工作,但是如果您要处理的是大型集合,则扩展性就不好。update_or_create始终首先运行SELECT,然后再运行UPDATE。

for the_bar in bars:
    updated_rows = SomeModel.objects.filter(bar=the_bar).update(foo=100)
        if not updated_rows:
            # if not exists, create new
            SomeModel.objects.create(bar=the_bar, foo=100)

这充其量仅会运行第一个更新查询,并且仅当它匹配零行时才运行另一个INSERT查询。如果您希望大多数行实际存在,那将大大提高您的性能。

但这全都取决于您的用例。如果您期望大部分插入内容,则可以选择bulk_create()命令。

For only a small amount of objects the update_or_create works well, but if you’re doing over a large collection it won’t scale well. update_or_create always first runs a SELECT and thereafter an UPDATE.

for the_bar in bars:
    updated_rows = SomeModel.objects.filter(bar=the_bar).update(foo=100)
        if not updated_rows:
            # if not exists, create new
            SomeModel.objects.create(bar=the_bar, foo=100)

This will at best only run the first update-query, and only if it matched zero rows run another INSERT-query. Which will greatly increase your performance if you expect most of the rows to actually be existing.

It all comes down to your use case though. If you are expecting mostly inserts then perhaps the bulk_create() command could be an option.


回答 4

我想我要添加一个答案,因为您的问题标题看起来像是在询问如何创建或更新,而不是按照问题正文中的描述进行获取或创建。

如果您确实想创建或更新对象,则默认情况下,.save()方法已经具有以下行为,来自docs

Django提取了使用INSERT或UPDATE SQL语句的需求。具体来说,当您调用save()时,Django遵循以下算法:

如果对象的主键属性设置为计算结果为True的值(即,无或空字符串以外的值),则Django将执行UPDATE。如果未设置对象的主键属性,或者UPDATE未更新任何内容,则Django执行INSERT。

值得注意的是,当他们说“如果UPDATE不更新任何内容”时,它们实际上是指您为对象提供的ID在数据库中不存在的情况。

Thought I’d add an answer since your question title looks like it is asking how to create or update, rather than get or create as described in the question body.

If you did want to create or update an object, the .save() method already has this behaviour by default, from the docs:

Django abstracts the need to use INSERT or UPDATE SQL statements. Specifically, when you call save(), Django follows this algorithm:

If the object’s primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes an UPDATE. If the object’s primary key attribute is not set or if the UPDATE didn’t update anything, Django executes an INSERT.

It’s worth noting that when they say ‘if the UPDATE didn’t update anything’ they are essentially referring to the case where the id you gave the object doesn’t already exist in the database.


回答 5

如果创建时的输入之一是主键,那么这就足够了:

Person.objects.get_or_create(id=1)

如果存在,它将自动更新,因为不允许两个具有相同主键的数据。

If one of the input when you create is a primary key, this will be enough:

Person.objects.get_or_create(id=1)

It will automatically update if exist since two data with the same primary key is not allowed.


Python / Django:登录到runserver下的控制台,登录到Apache下的文件

问题:Python / Django:登录到runserver下的控制台,登录到Apache下的文件

print在之下运行Django应用程序时manage.py runserver,如何将跟踪消息发送至控制台(如),而在Apache下运行应用程序时如何将这些消息发送至日志文件?

我回顾了Django日志记录,尽管它对高级用途的灵活性和可配置性给我留下了深刻的印象,但我仍然对如何处理简单的用例感到困惑。

How can I send trace messages to the console (like print) when I’m running my Django app under manage.py runserver, but have those messages sent to a log file when I’m running the app under Apache?

I reviewed Django logging and although I was impressed with its flexibility and configurability for advanced uses, I’m still stumped with how to handle my simple use-case.


回答 0

在mod_wsgi下运行时,打印到stderr的文本将显示在httpd的错误日志中。您可以print直接使用,也可以logging改用。

print >>sys.stderr, 'Goodbye, cruel world!'

Text printed to stderr will show up in httpd’s error log when running under mod_wsgi. You can either use print directly, or use logging instead.

print >>sys.stderr, 'Goodbye, cruel world!'

回答 1

这是一个基于Django日志记录的解决方案。它使用DEBUG设置,而不是实际检查您是否在运行开发服务器,但是如果您找到一种更好的检查方法,则应该很容易进行调整。

LOGGING = {
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/your/file.log',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

if DEBUG:
    # make all loggers use the console.
    for logger in LOGGING['loggers']:
        LOGGING['loggers'][logger]['handlers'] = ['console']

有关详细信息,请参见https://docs.djangoproject.com/en/dev/topics/logging/

Here’s a Django logging-based solution. It uses the DEBUG setting rather than actually checking whether or not you’re running the development server, but if you find a better way to check for that it should be easy to adapt.

LOGGING = {
    'version': 1,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/your/file.log',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

if DEBUG:
    # make all loggers use the console.
    for logger in LOGGING['loggers']:
        LOGGING['loggers'][logger]['handlers'] = ['console']

see https://docs.djangoproject.com/en/dev/topics/logging/ for details.


回答 2

您可以配置settings.py文件登录。

一个例子:

if DEBUG:
    # will output to your console
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
    )
else:
    # will output to logging file
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
        filename = '/my_log_file.log',
        filemode = 'a'
    )

但是,这取决于设置DEBUG,也许您不必担心它的设置方式。关于如何确定我的Django应用程序是否正在开发服务器上运行的信息,请参见此答案以更好的方式编写该条件。编辑:上面的示例来自Django 1.1项目,自该版本以来,Django中的日志记录配置已有所更改。

You can configure logging in your settings.py file.

One example:

if DEBUG:
    # will output to your console
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
    )
else:
    # will output to logging file
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
        filename = '/my_log_file.log',
        filemode = 'a'
    )

However that’s dependent upon setting DEBUG, and maybe you don’t want to have to worry about how it’s set up. See this answer on How can I tell whether my Django application is running on development server or not? for a better way of writing that conditional. Edit: the example above is from a Django 1.1 project, logging configuration in Django has changed somewhat since that version.


回答 3

我用这个:

logging.conf:

[loggers]
keys=root,applog
[handlers]
keys=rotateFileHandler,rotateConsoleHandler

[formatters]
keys=applog_format,console_format

[formatter_applog_format]
format=%(asctime)s-[%(levelname)-8s]:%(message)s

[formatter_console_format]
format=%(asctime)s-%(filename)s%(lineno)d[%(levelname)s]:%(message)s

[logger_root]
level=DEBUG
handlers=rotateFileHandler,rotateConsoleHandler

[logger_applog]
level=DEBUG
handlers=rotateFileHandler
qualname=simple_example

[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=applog_format
args=('applog.log', 'a', 10000, 9)

[handler_rotateConsoleHandler]
class=StreamHandler
level=DEBUG
formatter=console_format
args=(sys.stdout,)

testapp.py:

import logging
import logging.config

def main():
    logging.config.fileConfig('logging.conf')
    logger = logging.getLogger('applog')

    logger.debug('debug message')
    logger.info('info message')
    logger.warn('warn message')
    logger.error('error message')
    logger.critical('critical message')
    #logging.shutdown()

if __name__ == '__main__':
    main()

I use this:

logging.conf:

[loggers]
keys=root,applog
[handlers]
keys=rotateFileHandler,rotateConsoleHandler

[formatters]
keys=applog_format,console_format

[formatter_applog_format]
format=%(asctime)s-[%(levelname)-8s]:%(message)s

[formatter_console_format]
format=%(asctime)s-%(filename)s%(lineno)d[%(levelname)s]:%(message)s

[logger_root]
level=DEBUG
handlers=rotateFileHandler,rotateConsoleHandler

[logger_applog]
level=DEBUG
handlers=rotateFileHandler
qualname=simple_example

[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=applog_format
args=('applog.log', 'a', 10000, 9)

[handler_rotateConsoleHandler]
class=StreamHandler
level=DEBUG
formatter=console_format
args=(sys.stdout,)

testapp.py:

import logging
import logging.config

def main():
    logging.config.fileConfig('logging.conf')
    logger = logging.getLogger('applog')

    logger.debug('debug message')
    logger.info('info message')
    logger.warn('warn message')
    logger.error('error message')
    logger.critical('critical message')
    #logging.shutdown()

if __name__ == '__main__':
    main()

回答 4

您可以使用tagalog(https://github.com/dorkitude/tagalog)轻松完成此操作

例如,当标准python模块写入以附加模式打开的文件对象时,App Engine模块(https://github.com/dorkitude/tagalog/blob/master/tagalog_appengine.py)会覆盖此行为,而是使用logging.INFO

要在App Engine项目中获得此行为,只需执行以下操作:

import tagalog.tagalog_appengine as tagalog
tagalog.log('whatever message', ['whatever','tags'])

您可以自己扩展模块并覆盖日志功能,而不会遇到太大困难。

You can do this pretty easily with tagalog (https://github.com/dorkitude/tagalog)

For instance, while the standard python module writes to a file object opened in append mode, the App Engine module (https://github.com/dorkitude/tagalog/blob/master/tagalog_appengine.py) overrides this behavior and instead uses logging.INFO.

To get this behavior in an App Engine project, one could simply do:

import tagalog.tagalog_appengine as tagalog
tagalog.log('whatever message', ['whatever','tags'])

You could extend the module yourself and overwrite the log function without much difficulty.


回答 5

这在我的local.py中效果很好,省去了常规日志记录的麻烦:

from .settings import *

LOGGING['handlers']['console'] = {
    'level': 'DEBUG',
    'class': 'logging.StreamHandler',
    'formatter': 'verbose'
}
LOGGING['loggers']['foo.bar'] = {
    'handlers': ['console'],
    'propagate': False,
    'level': 'DEBUG',
}

This works quite well in my local.py, saves me messing up the regular logging:

from .settings import *

LOGGING['handlers']['console'] = {
    'level': 'DEBUG',
    'class': 'logging.StreamHandler',
    'formatter': 'verbose'
}
LOGGING['loggers']['foo.bar'] = {
    'handlers': ['console'],
    'propagate': False,
    'level': 'DEBUG',
}