Home > database >  error when Redirecting user to someone profile in other model
error when Redirecting user to someone profile in other model

Time:06-04

I want to redirected user to the profile page in the question model, I want when user click on url in the username of the question model, I want that url to redirected user to the author of that question to his/her public_profile page like facebook you can visit someone profile by clicking on his name when he/she post something, I have try using this method but it throws me an error: Reverse for 'Public_Profile' with arguments '('',)' not found. 1 pattern(s) tried: ['userProfile/(?P[-a-zA-Z0-9_] )/\Z']

this is what i have try:

my urls:

urlpatterns = [
    path('', views.rules, name='Rules'),
    path('create', views.login, name='login'),
    path('index/', views.index, name='index'),
    path('view/<slug:slug>/', views.viewQuestion, name='view-Question'),
    path('question/<int:pk>/answer/', views.My_Answer.as_view(), name='answer'),
    path('question/', views.My_Question.as_view(), name='question'),
    path('register/', views.register, name='register'),
    path('feedback/', views.PostFeedBack.as_view(), name='FeedBack'),
    path('notification/', views.NotificationListView.as_view(), name='notification'),
    path('profile/<int:pk>/', views.profile, name='Profile'),
    path('edit/<slug:slug>/', views.EditProfile.as_view(), name='edit'),
    path('userProfile/<slug:slug>/', views.public_profile, name='Public_Profile'),
]

my index/home template:

    <div >
    <div >
        {% for question in list_of_question reversed %}
        <div >
            <div >
                <div >
                <p ><a href="{% url 'Public_Profile' profile.slug %}">
                    {{question.user.username.upper}}
                </a></p>
                </div>
                <div >
                    <a href="{% url 'view-Question' question.slug %}" style="text-decoration: none;">
                        <p >{{question.title}}</p>
                    </a>
                    <h5>{{question.category}}</h5>
                </div>
            </div>
        </div>
        {%endfor%}
    </div>
</div>

my views:

@login_required(login_url='login')
def index(request):
    query = request.GET.get('q', None)
    list_of_question = Question.objects.all()
    if query is not None:
        list_of_question = Question.objects.filter(
            Q(title__icontains=query) |
            Q(category__name__icontains=query)
        )
    unread_notifications = 
    Notification.objects.filter(user=request.user, 
    is_read=False).count()

    paginator = Paginator(list_of_question, per_page=15)
    context = [
        {
             'unread_notifications':unread_notifications,
             'list_of_question':list_of_question,
             'paginator':paginator
       }
    ]
    return render(request, 'index.html', context)

def profile(request, pk):
    profiles = Profile.objects.filter(user=request.user)
    questions = Question.objects.filter(user=request.user)
    context = {
        'profiles':profiles,
        'questions':questions
    }
    return render(request, 'profile.html', context)

def public_profile(request, slug):
    contents = get_object_or_404(Profile, slug=slug)
    profiles = Profile.objects.filter(user=contents)
    context = {
        'contents':contents,
        'profiles':profiles
    }
    return render(request, 'public_profile.html', context)

my model

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    profile_image = models.ImageField(upload_to='avatar', blank=True, null=True, 
    default='static/default.jpg')
    stories = RichTextField(blank=True, null=True)
    twitter = models.URLField(max_length=300, blank=True, null=True)
    website = models.URLField(max_length=300,blank=True, null=True)
    city = models.CharField(max_length=50, blank=True, null=True)
    location = models.CharField(max_length=80, blank=True, null=True)
    slug = models.SlugField(unique=True, max_length=200)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.user)
        super(Profile, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.user)

class Question(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100, blank=False, null=False)
    body = RichTextField(blank=False, null=False) 
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    slug = models.SlugField(unique=True, max_length=200)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Question, self).save(*args, **kwargs)

    def __str__(self):
        return str(self.title)

CodePudding user response:

You should change profile.slug into question.user.profile.slug

Try this in your template:

<div >
    <div >
        {% for question in list_of_question reversed %}
        <div >
            <div >
                <div >
                <p >
                <a href="{% url'Public_Profile' question.user.profile.slug %}">
                    {{question.user.username.upper}}
                </a></p>
                </div>
                <div >
                    <a href="{% url 'view-Question' question.slug %}" style="text-decoration: none;">
                        <p >{{question.title}}</p>
                    </a>
                    <h5>{{question.category}}</h5>
                </div>
            </div>
        </div>
        {%endfor%}
    </div>
</div>

CodePudding user response:

In template

<p >
 <a href="/profile/{{question.user}}">
   {{question.user.username.upper}}
 </a>
</p>

Views:

def profile(request, pk):
    profiles = Profile.objects.get(user=pk)
    questions = Question.objects.filter(user=pk)
    context = {
        'profiles':profiles,
        'questions':questions
    }
    return render(request, 'profile.html', context)

CodePudding user response:

The error is trying to tell you that django tried to call Public_Profile but it requires some slug. This is because profile is None in the template context. You need to change <a href="{% url 'Public_Profile' profile.slug %}"> with <a href="{% url 'Public_Profile' question.user.slug %}">

  • Related