简体   繁体   English

部署到PythonAnyWhere后,出现NoReverseMatch错误

[英]After deploy to PythonAnyWhere get NoReverseMatch error

i'm trying the djangoGirls tutorial, and after doing the Extend Your Application, my site works just fine at local, on the http://127.0.0.1:8000/ . 我正在尝试djangoGirls教程,在完成“扩展您的应用程序”后,我的网站在本地http://127.0.0.1:8000/上运行良好 But when i pull to the PythonAnyWhere and try to acess the site, i got the error NoReverseMatch, like the picture below: 但是,当我拉到PythonAnyWhere并尝试访问该站点时,出现错误NoReverseMatch,如下图所示:

NoReverseMatch Error NoReverseMatch错误

So i want to know if anyone have a idea of what i can do to solve the problem, because i try and can't do it. 所以我想知道是否有人对我可以解决该问题有一个想法,因为我尝试并且无法做到。 I have no idea how to make it work. 我不知道如何使它工作。

All my codes are similar to the tutorial, here they : 我所有的代码都与本教程类似,在这里:

urls.py: urls.py:

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

 urlpatterns = [
     url(r'^$', views.post_list),
     url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
 ]

views.py views.py

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Post

# Create your views here.

def post_list(request):
    posts = Post.objects.filter(published_date__lte = timezone.now()).order_by('published_date')
    return render(request, 'blog/post_lists.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

models.py: models.py:

from django.db import models
from django.utils import timezone

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created_date = models.DateTimeField(default = timezone.now)
    published_date = models.DateTimeField(blank = True, null = True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

base.html base.html

{% load staticfiles %}
<html>
<head>
    <title>Django Girls blog</title>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
    <link href='//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
    <div class="page-header">
        <h1><a href="/">Django Girls Blog</a></h1>
    </div>
    <div class="content container">
        <div class="row">
            <div class="col-md-8">
                {% block content %}
                {% endblock %}
            </div>
        </div>
    </div>

post_lists.html: post_lists.html:

{% extends 'blog/base.html' %}

{% block content %}
    {% for post in posts %}
        <div class="post">
            <div class="date">
                {{ post.published_date }}
            </div>
            <h1><a href="{% url 'blog.view.post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
            <p>{{ post.text|linebreaksbr }}</p>
        </div>
    {% endfor %}
{% endblock content %}

post_detail.html: post_detail.html:

{% extends 'blog/base.html' %}

{% block content %}
    <div class="post">
        {% if post.published_date %}
            <div class="date">
                {{ post.published_date }}
            </div>
        {% endif %}
        <h1>{{ post.title }}</h1>
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
{% endblock %}

The link to the project on github is github.com/lucasdaquina/my-first-blog github上项目的链接是github.com/lucasdaquina/my-first-blog

Sorry not put a link on that, i can't put more than one link yet. 抱歉,没有在该链接上放置链接,我不能放多个链接。

If i need to put some other information necessary to help me, just let me now. 如果我需要提供其他必要的信息来帮助我,请现在就让我来。 Thank you all for the help and attention. 谢谢大家的帮助和关注。

As you follow the Django Girls tutorial , you should get a NoReverseMatch error at first. 当您遵循Django Girls教程时 ,首先应该出现NoReverseMatch错误。 It then shows you how to fix it. 然后,它向您展示如何修复它。

First of all, the url tag should just have 'post_detail' , not 'blog.view.post_detail' as you have. 首先,url标记应该只具有'post_detail' ,而不是'blog.view.post_detail'

{% url 'post_detail' pk=post.pk %}"

Then, the tutorial gets you to add a name to your URL pattern to fix the error: 然后,本教程使您可以在URL模式中添加一个名称来修复错误:

 url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name="post_detail"),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM