0

I'm currently on the last project for CS50 Web called Network and am stuck on the Follower spec. I'm trying to get the url in the Following tab to bring you to the profile of the specific user that's clicked on. For some reason it doesn't work on followers.html but works totally fine for another template.

Here's the error message and traceback: enter image description here

Internal Server Error: /followers
Traceback (most recent call last):
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/simone/Desktop/project4 2/network/views.py", line 150, in followers
    return render(request, "network/followers.html", {"posts":posts})
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/template/defaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/simone/Library/Python/3.7/lib/python/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'profile' with arguments '('',)' not found. 1 pattern(s) tried: ['profile/(?P<username>[^/]+)$']
[23/Oct/2020 07:04:58] "GET /followers HTTP/1.1" 500 161362

Here are the models:

class User(AbstractUser):
    pass

class New_Post(models.Model):
    objects = models.Manager()
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=None, null=True, blank=True, related_name="posts")
    post = models.CharField(max_length=200)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.post}"

class Following(models.Model):
    objects = models.Manager()
    user = models.ForeignKey(User, on_delete=models.CASCADE, default=None, null=True, blank=True, related_name="current_user")
    followers = models.ForeignKey(User, on_delete=models.CASCADE, related_name="followers", blank=True, default=None, null=True)
    following = models.ForeignKey(User, on_delete=models.CASCADE, related_name="following", blank=True, default=None, null=True)

    def __str__(self):
        return f"User:{self.user} follows {self.following} and {self.followers} follow him/her."

urls.py:

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("new_post", views.new_post, name="new_post"),
    path("profile/<str:username>", views.profile, name="profile"),
    path("followers", views.followers, name="followers")
]

views.py

@login_required
def followers(request):
    user = User.objects.get(username=request.user)
    follows = user.followers.all()

    all_posts = New_Post.objects.all().order_by('timestamp').reverse()
    posts = []
    for post in all_posts:
        for follower in follows:
            if follower.user == post.user:
                posts.append(post.user)
                posts.append(post)
    return render(request, "network/followers.html", {"posts":posts})

followers.html:

{% extends "network/layout.html" %}

{% block body %}
<h2 style="font-family: 'Crimson Text', serif; text-align: center;">Peple You Follow</h2>
<br>
    {% for post in posts %}
    <div class="container shadow-lg p-3 mb-5 bg-white rounded">
        <h4 style="font-family: 'Crimson Text', serif;"><a href="{% url 'profile' post.user %}">{{post.user}}</a></h4>
        <hr>
        <p class="text"> {{post.post}} </p>
        <br>
        <p> Likes </p>
        <p style="font-size:10px;"> Posted on: {{post.timestamp}} </p>
        <hr>
    </div>
    {% endfor %}
{% endblock %}

Thanks for your help!

You must log in to answer this question.

Browse other questions tagged .