2016-11-15 133 views
0

要麼我在我的項目中得到一個奇怪的URL錯誤,或者我失去了一些東西。NoReverseMatch在Django

我想獲取配置文件ID並在名爲「profile.html」的模板中顯示信息。很簡單,不是嗎?

但我每次調用此「配置文件url」時都會收到NoReverseMatch錯誤。

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"), 
] 

我的檔案查看:

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) 

我的登錄用戶重定向視圖:

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}) 

我收到此錯誤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+)/$'] 

我不知道該怎麼做,幫助:)

回答

3

您應該通過sugarid作爲kwarg本身。這應該工作:

return redirect("profile", sugarid=request.user.id) 
+0

這個工作,但在現在我'越來越NoReverseMatch錯誤我所有的其他網址。這是煩我幾天:( 他試圖模式「個人資料」,我不知道爲什麼大聲笑 – Storm

+0

你可以詳細一點嗎? – v1k45

+0

沒關係,發現我的錯誤,我包括「header.html 「這是與所有模板中的網址」個人資料「,但沒有傳遞參數時渲染。感謝您的時間夥伴。:) – Storm

1

您可以通過位置或關鍵字參數,因爲它是:

redirect("profile", sugarid=request.user.id) 

檢查文檔: redirect in Django

+0

這工作爲@ v1k45說,但現在我得到NoReverseMatch錯誤在我所有的其他URL 。 – Storm

+0

請提供更多詳情。哪些網址失敗,什麼時候失敗,請更新您的問題或添加新的問題! – aliasav

+0

我已評論@ v1k45回答我自己的錯誤,謝謝你的回答! – Storm