簡體   English   中英

如何在Django中獲取外鍵的特定字段?

[英]How to get specific fields of a Foreign key in django?

因此,我一直在學習django,因此決定創建一個帶有評論的博客。 這是我的模型:

class Author(models.Model):

    name = models.CharField(max_length = 20)
    email = models.EmailField(verbose_name = 'e-mail')

    def __str__(self):
        return self.name


class Post(models.Model):

    author = models.ForeignKey(Author)
    title = models.CharField(max_length=80)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def __str__(self):
        return self.title

class Comment(models.Model):

    author = models.OneToOneField(Author)
    post = models.ForeignKey(Post)
    text = models.TextField()
    post_date = models.DateTimeField(default = timezone.now)

    def __str__(self):
        return self.author + "commented"

現在,在我的模板中,我無法訪問作者的姓名。它只是隱藏了注釋。{{comment.author.name}}不起作用。

這是模板塊。

{% extends "base.html" %}

{% block content %}
<div class="post">
    {% if post.published_date %}
        <div class="date">
            {{ post.published_date }}
        </div>
    {% endif %}
    <a class="btn btn-default" href="{% url "post_edit" pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
    <h1>{{ post.title }}-{{post.author}}</h1>
    <p>{{ post.text|linebreaks }}</p>
</div>
{% for comment in comments %}
    <div class="post">
        <div class="date">
            {{ comment.post_date }}
        </div>
        <h4>Author:{{comment.author.name}}</h4>
        <h5>{{ comment.text|linebreaks }}</h5>
    </div>
{% endfor %}
<h3>New Comment</h3>
<form method="POST" class="post-form">{% csrf_token %}
    {{ form.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}

ViewCode

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            author = Author.objects.create()
            author.name = request.user
            author.email = ""
            comment.author = author
            comment.post = post
            comment.post_date = timezone.now()
            comment.save()
            return redirect('/posts/'+pk+'/')

    else:
        form = CommentForm()
    comments = Comment.objects.order_by('post_date')    
    return render(request, 'post_detail.html', {'post': post, 'comments':      comments, 'form': form})

問題在這里:

author = Author.objects.create()
author.name = request.user
author.email = ""
comment.author = author

您創建的作者,但不保存名稱和電子郵件。

嘗試這個:

author = Author.objects.create(name=request.user, email="")
comment.author = author

或這個:

author = Author.objects.create()
author.name = request.user
author.email = ""
author.save()
comment.author = author.pk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM