Reputation: 59
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".
My urls.py
:
from django.conf.urls import url, include
from django.contrib import admin
from sugar import views
urlpatterns = [
url(r'^area51/', admin.site.urls),
url(r'^search/', views.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:
midList.append(mid)
if len(midList) < 4:
result = 4 - len(midPhotos)
for r in range(result):
midList.append(None)
lastPhotos = queryPhotos[1:4]
for last in lastPhotos:
lastList.append(last)
if len(lastList) < 3:
result = 3 - len(lastPhotos)
for r in range(result):
lastList.append(None)
variables['name'] = user.name
variables['status'] = user.status
variables['description'] = user.description
variables['whatyouwant'] = user.whatyouwant
variables['state'] = user.state
variables['city'] = user.city
if featuredPhoto:
variables['featuredPhoto'] = featuredPhoto['photo']
else:
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': request.user.id})
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 :)
Upvotes: 0
Views: 464
Reputation: 3168
You can pass positional or keyword arguments as it is:
redirect("profile", sugarid=request.user.id)
Check the docs: redirect in Django
Upvotes: 1
Reputation: 8250
You should pass sugarid
as a kwarg itself. This should work:
return redirect("profile", sugarid=request.user.id)
Upvotes: 3