Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:从getter方法返回的models.CharField()值为空_Python_Django_Django Models_Django Templates - Fatal编程技术网

Python Django:从getter方法返回的models.CharField()值为空

Python Django:从getter方法返回的models.CharField()值为空,python,django,django-models,django-templates,Python,Django,Django Models,Django Templates,这是我的模型课。在本文中,我添加了一个新的getter方法url(),用于返回作者配置文件的url字符串。但当我在html模板文件中调用这个方法时,它显示为空 请看以下是我的使用示例: 作者模型类: from django.db import models import re class Author(models.Model): salutation = models.CharField(max_length=10) name = models.CharField(max_l

这是我的模型课。在本文中,我添加了一个新的getter方法
url()
,用于返回作者配置文件的url字符串。但当我在html模板文件中调用这个方法时,它显示为空

请看以下是我的使用示例:

作者模型类:

from django.db import models
import re

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    name = models.CharField(max_length=64)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='author_headshots')

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

    @property
    def url(self):
        return re.sub(r"[\s|\.|\-|\_|\'|\+]+", "-", self.name)
from django.shortcuts import render

from author.models import Author

def index(request):
    authors = Author.objects.order_by('-name')
    return render(request, 'home.html', {
                                         'authors': authors
                                         })
{% if authors %}
    {% for author in authors %}
    <h2><a href="author/{{ author.url }}/">{{ author.name }}</a></h2>
    <p>About author {{ author.name }} here</p>
    {% endfor %}
{% else %}
No Authors
{% endif %}
<h2><a href="author//">XXX</a></h2>
<p>About author XXX here</p>
...
<h2><a href="author/xxx/">XXX</a></h2>
<p>About author XXX here</p>
...
作者
对象上下文从视图方法传递到模板:

from django.db import models
import re

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    name = models.CharField(max_length=64)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='author_headshots')

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

    @property
    def url(self):
        return re.sub(r"[\s|\.|\-|\_|\'|\+]+", "-", self.name)
from django.shortcuts import render

from author.models import Author

def index(request):
    authors = Author.objects.order_by('-name')
    return render(request, 'home.html', {
                                         'authors': authors
                                         })
{% if authors %}
    {% for author in authors %}
    <h2><a href="author/{{ author.url }}/">{{ author.name }}</a></h2>
    <p>About author {{ author.name }} here</p>
    {% endfor %}
{% else %}
No Authors
{% endif %}
<h2><a href="author//">XXX</a></h2>
<p>About author XXX here</p>
...
<h2><a href="author/xxx/">XXX</a></h2>
<p>About author XXX here</p>
...
并在模板(home.html)中使用以下命令:

from django.db import models
import re

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    name = models.CharField(max_length=64)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='author_headshots')

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

    @property
    def url(self):
        return re.sub(r"[\s|\.|\-|\_|\'|\+]+", "-", self.name)
from django.shortcuts import render

from author.models import Author

def index(request):
    authors = Author.objects.order_by('-name')
    return render(request, 'home.html', {
                                         'authors': authors
                                         })
{% if authors %}
    {% for author in authors %}
    <h2><a href="author/{{ author.url }}/">{{ author.name }}</a></h2>
    <p>About author {{ author.name }} here</p>
    {% endfor %}
{% else %}
No Authors
{% endif %}
<h2><a href="author//">XXX</a></h2>
<p>About author XXX here</p>
...
<h2><a href="author/xxx/">XXX</a></h2>
<p>About author XXX here</p>
...
{%if作者%}
{作者中的作者%}
关于作者{{author.name}这里

{%endfor%} {%else%} 没有作者 {%endif%}
获取输出:

from django.db import models
import re

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    name = models.CharField(max_length=64)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='author_headshots')

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

    @property
    def url(self):
        return re.sub(r"[\s|\.|\-|\_|\'|\+]+", "-", self.name)
from django.shortcuts import render

from author.models import Author

def index(request):
    authors = Author.objects.order_by('-name')
    return render(request, 'home.html', {
                                         'authors': authors
                                         })
{% if authors %}
    {% for author in authors %}
    <h2><a href="author/{{ author.url }}/">{{ author.name }}</a></h2>
    <p>About author {{ author.name }} here</p>
    {% endfor %}
{% else %}
No Authors
{% endif %}
<h2><a href="author//">XXX</a></h2>
<p>About author XXX here</p>
...
<h2><a href="author/xxx/">XXX</a></h2>
<p>About author XXX here</p>
...

关于作者XXX

...
期望:

from django.db import models
import re

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    name = models.CharField(max_length=64)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='author_headshots')

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

    @property
    def url(self):
        return re.sub(r"[\s|\.|\-|\_|\'|\+]+", "-", self.name)
from django.shortcuts import render

from author.models import Author

def index(request):
    authors = Author.objects.order_by('-name')
    return render(request, 'home.html', {
                                         'authors': authors
                                         })
{% if authors %}
    {% for author in authors %}
    <h2><a href="author/{{ author.url }}/">{{ author.name }}</a></h2>
    <p>About author {{ author.name }} here</p>
    {% endfor %}
{% else %}
No Authors
{% endif %}
<h2><a href="author//">XXX</a></h2>
<p>About author XXX here</p>
...
<h2><a href="author/xxx/">XXX</a></h2>
<p>About author XXX here</p>
...

关于作者XXX

...
我想我有些东西。与其定义模型的属性,不如定义如下全局函数(在视图文件中):

然后在索引视图中添加以下内容:

for author in authors:
    url(author)
    #print author.url

您是否检查了您的
re.sub
调用是否确实返回了预期的字符串?除了空字符串问题之外:您是否查看了,并且?re.sub没有问题。显示为空,即使我返回
self.name
也只返回
re.sub(r“[\s\.\.\-\\\\\\\\'\\+]+,“-”,self.name)
@alko是的,我已将author对象传递给template请显示视图代码。实际上不是。因为从
url()
方法返回的字符串对于不同的模型类型是不同的。类似于Post
url()
将返回类似以下内容:“/news/2013/11/06/news Post title as slug/440/”@VINAYKr.SHARMA抱歉,我以为您只需要针对作者模型的解决方案。如果您需要其他内容,请编辑您的问题。。。