【问题标题】:NoReverseMatch at /blog Reverse for 'single_blog' with no arguments not found. 1 pattern(s) tried: ['single_blog/(?P<id>[0-9]+)/$']NoReverseMatch at /blog 为“single_blog”反向匹配,未找到任何参数。尝试了 1 种模式:['single_blog/(?P<id>[0-9]+)/$']
【发布时间】:2021-05-11 02:48:24
【问题描述】:

我尝试了很多次,但页面没有呈现,我不明白我做错了什么?你能告诉我,我哪里做错了吗?

models.py

class Post(models.Model):
head = models.CharField(blank=False, unique=True, max_length=250)
date_time = models.DateTimeField(blank=True, null=True, default=None)
description = models.TextField(blank=False, max_length=1000)
by_name = models.CharField(blank=False, null=True, unique=True, max_length=250)
by_img = models.ImageField(null=True, blank=True)

def __str__(self):
    return self.head

views.py

def blog(request):
blogs = Post.objects.all()
return render(request, 'blog.html', {'blogs': blogs})

def blog(request):
blogs = Post.objects.all()
return render(request, 'blog.html', {'blogs': blogs})

def 动态博客(请求,ID): obj = Post.objects.get(id=id) return render(request, 'single_blog.html', {'obj': obj})

urls.py

from django.urls import path
from . import views

urlpatterns = [
path('', views.home, name='home'),
path('contact', views.contact, name='contact'),
path('shop', views.shop, name='shop'),
path('service', views.service, name='service'),
path('blog', views.blog, name='blog'),
path('single_blog/<int:id>/', views.dynamicblog, name='single_blog'),
path('single_service', views.single_service, name='single_service'),

blog.html

{% for blog in blogs %}
            <div class="col-lg-4 col-md-6 col-sm-12 news-block">
                <div class="news-block-two news-block-three wow fadeInLeft" data-wow-delay="300ms" data-wow-duration="1500ms">
                    <div class="inner-box">
                        <div class="image-holder">
                                <figure class="image-box"><img src="{{blog.by_img.url}}" alt=""></figure>
                        </div>
                        <div class="lower-content">
                            <h4><a href="{% url 'single_blog' %}">{{blog.head}}</a></h4>
                            <div class="post-info">by {{blog.by_name}} on {{blog.date_time}}</div>
                            <div class="text">{{blog.description}}</div>
                            <div class="link-btn"><a href="{% url 'single_blog/{{blog.id}}' %}">Read More<i class="flaticon-slim-right"></i></a></div>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}

single_blog.html

<div class="col-lg-4 col-md-12 col-sm-12 sidebar-side">
                {% for blog in obj %}
                <div class="sidebar blog-sidebar">
                    <div class="contact-widget sidebar-widget wow fadeInLeft" data-wow-delay="00ms" data-wow-duration="1500ms">
                        <div class="widget-content">
                            <h4>{{blog.head}}</h4>
                            <div class="text">Our industrial plant services keep your generator up</div>
                            <div class="btn-box"><a href="{% url 'contact' %}"><i class="fas fa-angle-right"></i>Contact Agent</a></div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>

【问题讨论】:

    标签: python django django-models django-views django-templates


    【解决方案1】:

    您的 single_blog 路径需要一些参数。所以通过它:

    blog.html

    {% for blog in blogs %}
    
    <div class="col-lg-4 col-md-6 col-sm-12 news-block">
      <div class="news-block-two news-block-three wow fadeInLeft" data-wow-delay="300ms" data-wow-duration="1500ms">
        <div class="inner-box">
          <div class="image-holder">
            <figure class="image-box"><img src="{{blog.by_img.url}}" alt=""></figure>
          </div>
          <div class="lower-content">
            <!--                              |  Here            -->
            <!--                              v                  -->
            <h4><a href="{% url 'single_blog' id=blog.id %}">{{blog.head}}</a></h4>
            <div class="post-info">by {{blog.by_name}} on {{blog.date_time}}</div>
            <div class="text">{{blog.description}}</div>
            <div class="link-btn">
              <!--                              |  Here            -->
              <!--                              v                  -->
              <a href="{% url 'single_blog' id=blog.id %}">Read More<i class="flaticon-slim-right"></i>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    {% endfor %}
    

    Refs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-13
      • 2021-12-12
      • 2019-05-10
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2019-11-01
      相关资源
      最近更新 更多