找不到Django网址页面

时间:2018-08-16 04:01:59

标签: python html django url

因此,我一直在尝试访问为网站创建的项目帖子,但是每次单击它都找不到URL。我不确定为什么。

我的代码如下:

mywebsite / urls.py

from django.contrib import admin
from django.urls import re_path, include
from django.conf import settings
from django.conf.urls.static import static

#re_path(r'^admin/', admin.site.urls),
urlpatterns = [
    re_path(r'^admin/', admin.site.urls),
    re_path(r'^', include('home.urls')),
    re_path(r'^projects/', include('projects.urls', namespace="create_post")),
    re_path(r'^contact/', include('contact.urls')),
]

projects / urls.py

from django.urls import re_path, include
from . import views

# urls for projects page
app_name = 'create_post'

urlpatterns = [
    re_path(r'^$', views.retrieve_projects, name="retrieve_projects"),
    #re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
    re_path(r'^create/$', views.CreateProjectsView.as_view(), name="create_projects"),
    re_path(r'^(?P<slug>[\w-]+)/$', views.details_of_project, name="details_of_project"),
    re_path(r'^(?P<slug>[\w-]+)/update/$', views.update_projects, name="update_projects"),
    re_path(r'^(?P<slug>[\w-]+)/delete/$', views.delete_projects, name="delete_projects"),
]

# To make images work
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

projects / views.py

# Function to retrieve all different projects
def retrieve_projects(request):
    # Retrieves objects based on latest publish date
    projects_list = Projects.objects.all().order_by("-publish_date")
    context = {
        'projects_list': projects_list,
    }
    return render(request, 'projects/projects.html', context)

projects / projects.html

<h1>asdasdasdasdasdasdas</h1>
{% if projects_list %}
<ul>
    {% for project in projects_list %}
    <h2><a href="/{{project.slug}}/">{{project.title}}</a></h2>
    <h2>{{project.description}}</h2>
    <h2>{{ project.publish_date }}</h2>

    {% endfor %}
</ul>
{% endif %}

找不到错误返回页面

1 个答案:

答案 0 :(得分:0)

尝试这个<a href={% url 'create_post:details_of_project' project.slug %}"></a>