django:当我点击text1页面时,该页面没有跳转到我的文章

时间:2018-04-02 02:28:30

标签: python django

当我点击文字1时,页面没有响应 这是网址:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'$',views.blog_title,name='blog_title'),
    url(r'(?P<article_id>\d)/$',views.blog_article,name='blog_article'),
]

这是观点:

def blog_article(request,article_id):
  article = models.BlogArticle.objects.get(id = article_id)
  return render(request,'blog/article.html',{'article':article})

这是html:

<div>
   <a>{{ article.body }}</a>
</div>

这是html:

<div>
<ul>
    {% for blog in blogs %}
    <li>
        <a href="{{ blog.id }}">
            {{ blog.title }}
        </a>
    </li>
    {% endfor %}
</ul>

这是模特:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class BlogArticle(models.Model):
    title = models.CharField(max_length=300)
    author = models.ForeignKey(User,related_name='blog_post',on_delete=models.DO_NOTHING)
    body = models.TextField()
    pub = models.DateTimeField(default=timezone.now)
    def __str__(self):
        return self.title

我的text1数据:enter image description here

1 个答案:

答案 0 :(得分:0)

将您的HTML代码更改为

<a href="{% url 'blog_article' blog.id %}">
    {{ blog.title }}
</a>

将您的网址更改为

url(r'(?P<article_id>\[0-9]+)/$',views.blog_article,name='blog_article'),