-2

so I been working on this for a couple of days now and I still dont seem to understand the problem. I am trying to get the username in the url so that you can just look up any user if you type http://127.0.0.1:8000/accounts/profile/username and that users profile shows up. I have looked at other questions but I keep getting this error when I try. If anyone could help me I would be greatly appreciated.

NoReverseMatch at /accounts/profile/admin Reverse for 'viewprofile' with arguments '('',)' not found. 1 pattern(s) tried: ['accounts/profile/(?P[\w.@+-]+)']

views.py

def view_profile(request, username):
    username = User.objects.get(username=username)
    posts = Post.objects.filter(author = request.user).order_by('-pub_date')
    args = {
    'user': request.user,
    'posts': posts,

    }
    return render(request, 'accounts/profile.html', args)

in my nav bar: this could be wrong but im sure its right

<li><a href="{% url 'accounts:viewprofile' username%}">Profile</a></li>

urls.py

    url(r'^profile/(?P<username>[\w.@+-]+)', views.view_profile, name="viewprofile"),
Mohamed
  • 75
  • 8
  • What URL are you trying to access? – Anna Jeanine May 02 '17 at 12:47
  • http://127.0.0.1:8000/accounts/profile/admin, That url there. I made an account named admin so Im just trying to view the profile for it by having accounts/profile/admin or even with a different account I want to view it by just doing accounts/profile/admin2 – Mohamed May 02 '17 at 12:51
  • Your error indicates that the `username` in that template tag has a null value, maybe you can show us the view that renders the nav bar so we can give an accurate answer – Dean Christian Armada May 02 '17 at 13:20

2 Answers2

0

You give user key in your context, you use username in your template. Maybe that's the problem.

albar
  • 3,020
  • 1
  • 14
  • 27
  • 1
    So please update your question, because as you ask it, the problem is what I pointed to. – albar May 02 '17 at 12:50
0

The URL which you're trying to match to if failing, please try this:

url(r'profile/(?P<username>[a-zA-Z0-9]+)$', views.view_profile, name="viewprofile"),
Anna Jeanine
  • 3,975
  • 10
  • 40
  • 74