Storm Storm - 1 year ago 327
Python Question

NoReverseMatch at django

Either I'm getting a weird URL error in my project or I'm missing something.

I want to get the profile ID and show the informations in a template called "profile.html". Quite simple isn't it?

But I'm getting a NoReverseMatch error every time I call this "profile url".


from django.conf.urls import url, include
from django.contrib import admin
from sugar import views

urlpatterns = [
url(r'^search/',, name="search"),
url(r'^terms/', views.terms, name="terms"),
url(r'^thanks/', views.thanks, name="thanks"),
url(r'^create_user/', views.create_user, name="create_user"),
url(r'^profile_edit/', views.profile_edit, name="profile_edit"),
url(r'^upload_photos/', views.photo_upload, name="photo_upload"),
url(r'^recover/', views.recover_account, name="recover"),
url(r'^login/', views.log_user_in, name="login"),
url(r'^logout/', views.log_user_out, name="logout"),
url(r'^register/', views.register, name="register"),
url(r'^profile/(?P<sugarid>\d+)/$', views.profile, name="profile"),
url(r'^payment/', views.payment, name="payment"),
url(r'^home', views.home, name="home"),
url(r'^paypal/', include('paypal.standard.ipn.urls')),
url(r'^$', views.home, name="home"),

My profile view:

def profile(request, sugarid):
if not request.user.is_authenticated():
return redirect("home")

variables = {}
exists = SugarUser.objects.filter(user_id=sugarid)
if exists:
user = SugarUser.objects.get(user_id=sugarid)
if not check_payment(user):
return redirect("payment")

midList = []
lastList = []

queryPhotos = UserPhoto.objects.filter(user_id=sugarid).order_by("-id")[:8]
featuredPhoto = queryPhotos.values().first()
midPhotos = queryPhotos[4:]
for mid in midPhotos:
if len(midList) < 4:
result = 4 - len(midPhotos)
for r in range(result):

lastPhotos = queryPhotos[1:4]
for last in lastPhotos:
if len(lastList) < 3:
result = 3 - len(lastPhotos)
for r in range(result):
variables['name'] =
variables['status'] = user.status
variables['description'] = user.description
variables['whatyouwant'] = user.whatyouwant
variables['state'] = user.state
variables['city'] =
if featuredPhoto:
variables['featuredPhoto'] = featuredPhoto['photo']
variables['featuredPhoto'] = None
variables['midPhotos'] = midList
variables['lastPhotos'] = lastList
variables['user'] = sugarid

return render(request, "profile.html", variables)

My login user redirection view:

def log_user_in(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return redirect("profile", kwargs={'sugarid':})

I am getting this error ALWAYS:

django.core.urlresolvers.NoReverseMatch: Reverse for 'profile' with arguments '()' and keyword arguments '{'kwargs': {'sugarid': 24}}' not found. 1 pattern(s) tried: ['profile/(?P<sugarid>\\d+)/$']

I don't know what to do anymore, help :)

Answer Source

You should pass sugarid as a kwarg itself. This should work:

return redirect("profile",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download