1

Hi I'm trying to view profile page at articles/profile/<int:author_id>

When I add profile page link, navbar doesn't work. If I remove articles:profile article.author_id from navbar.html all links works. My Error Message is here:

NoReverseMatch at / Reverse for 'profile' with arguments '('',)' not found. 1 pattern(s) tried: ['articles/profile/(?P<author_id>[0-9]+)$']

views.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})

navbar.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>

urls.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 %}

models.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
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • Error message is text, not picture, please add error details as text to your question. Screenshot might help as an additional info. https://stackoverflow.com/help/how-to-ask – Ivan Starostin Mar 03 '21 at 10:19
  • sorry, this my first question thank u for attention :) – mustafa bilgici Mar 03 '21 at 10:29
  • Url config and usage in the template seem to be fine, `article` or `article.author_id` is null - please check if the view passes values correctly, if the data is correct. – Ivan Starostin Mar 03 '21 at 11:35
  • You need to check some other view - not profile page. The view where navbar with given link shows up at first time. Index page maybe. – Ivan Starostin Mar 03 '21 at 11:36
  • @mustafabilgici I believe you use this navbar in many of your templates (by including it in some base template). Now you try to put a link in this navbar but one thing you should note is that you don't necessarily pass `article` into the context for all your views. Considering this I would advice that this url should not really be in your navbar if that is the case. – Abdul Aziz Barkat Mar 03 '21 at 12:09

0 Answers0