1

嗨,我正在尝试查看个人资料页面articles/profile/<int:author_id>

当我添加个人资料页面链接时,导航栏不起作用。如果我articles:profile article.author_idnavbar.html所有链接中删除有效。我的错误信息在这里:

NoReverseMatch at / Reverse for 'profile' with arguments '('',)' 未找到。尝试了 1 种模式:['articles/profile/(?P<author_id>[0-9]+)$']

视图.py

def profile(request,author_id):
    article = get_object_or_404(Article,author_id = author_id)
    context = {
        "article":article,
    }

    form = ArticleForm(request.POST or None,request.FILES or None)

    if form.is_valid():
        article = form.save(commit=False)
        
        article.author = request.user
        article.save()
        
        messages.success(request,"Dosya Yüklendi")
        return redirect("article:dashboard")

    return render(request,"profile.html",{"article":article,'form':form})

导航栏.html

<nav class="navbar navbar-toggleable-md navbar-inverse fixed-top bg-inverse">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="{% url 'index'  %}">YB Blog</a>
    
    <div class="collapse navbar-collapse" id="navbarCollapse">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="{% url 'about'  %}">Hakkımızda</a>
        </li>
        <li class="nav-item active">
            <a class="nav-link" href="{% url 'article:articles'  %}">Makaleler</a>
        </li>
      </ul>

      <ul class="navbar-nav ml-auto">
      {% if request.user.is_authenticated %}

      <li class="nav-item active">
        <a class="nav-link" href="{% url 'article:dashboard'  %}">Makale Ekle</a>
      </li>
      
      <li class="nav-item active">
        <a class="nav-link" href="{% url 'article:profile' article.author_id %}">Makale Ekle</a>
      </li>

      <li class="nav-item active">
        <a class="nav-link" href="{% url 'user:logout'  %}">Çıkış Yap</a>

      {% else %}

      <li class="nav-item active">
        <a class="nav-link" href="{% url 'user:login'  %}">Giriş Yap</a>
      </li>
      {% endif %}
      <li class="nav-item active">
        <a class="nav-link" href="{% url 'user:register'  %}">Kayıt Ol</a>
      </li>
      </ul>
    </div>
</nav>

网址.py

from django.contrib import admin
from django.urls import path
from . import views
app_name = "article"

urlpatterns = [
    path('profile/<int:author_id>',views.profile,name = "profile"),
    path('dashboard/',views.dashboard,name = "dashboard"),
    path('addarticle/',views.addArticle,name = "addarticle"),
    path('article/<int:id>',views.detail,name = "detail"),
    path('update/<int:id>',views.updateArticle,name = "update"),
    path('delete/<int:id>',views.deleteArticle,name = "delete"),
    path('',views.articles,name = "articles"),
    path('comment/<int:id>',views.addComment,name = "comment"),
    
]

profile.html

{% extends 'layout.html' %}

{% block body %}

<h1>Profile Sayfası<h1>

<small> Merhaba Hoşgeldin, {{article.author.username}}<small>
<hr>
<p>
</p>

{% if article.author.username == "admin" %}
<h2>Admin Hakları:</h2>
<small>Buradan Dosya Yükleyebilirsin</small>
<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Dosya Yükle</button>
  </form>
{% endif %}
{% endblock body %}

模型.py

from django.db import models
from ckeditor.fields import RichTextField

class Article(models.Model):
    author = models.ForeignKey("auth.User",on_delete = models.CASCADE,verbose_name = "Yazar ")
    title = models.CharField(max_length = 50,verbose_name = "Başlık")
    content = RichTextField()
    created_date = models.DateTimeField(auto_now_add=True,verbose_name="Oluşturulma Tarihi")
    article_image = models.FileField(blank = True,null = True,verbose_name="Makaleye Fotoğraf Ekleyin")
    def __str__(self):
        return self.title
4

0 回答 0