2017-02-17 64 views
0

views.py爲什麼我在get_absolute_url方法得到反向匹配錯誤 - ?Django的

class ProfileView(DetailView): 
    model = User 
    template_name ="profile/profile_view.html" 

class ProfileEdit(UpdateView): 
    model = User 
    form_class = ProfileForm 
    template_name="profile/profile_new.html" 

models.py

#.... models 
def get_absolute_url(self): 
    return reverse('user:user_profile', kwargs={ "slug": self.slug }) 

urls.py

url(r'^(?P<slug>[\[email protected]+-]+)/edit/$', ProfileEdit.as_view(), name='profile_edit'), 
url(r'^(?P<slug>[\[email protected]+-]+)/$', ProfileView.as_view(), name='user_profile'), 

反向的「 user'profile'帶有參數'()'和關鍵字參數'{'slug':'rahul'}'找不到。 0模式嘗試:[]

我不明白我得到正確的slug值,但什麼是正確的方式來傳遞它的url模式?

+0

是在安裝的應用程序設置,包括那些網址,您的應用程序? – Sayse

+0

是的!詳細視圖和更新視圖都可以正常工作..在更新視圖中的發佈請求之後,我想重定向到detailview – Rahul

+0

並且'user'命名空間肯定存在? (請包括相關的基地址) – Sayse

回答

1

當使用include你需要指定一個namespace,不是name

url(r'^', include('user.urls', namespace='user')) 
+0

它可以在模型中工作,但我如何在'settings .py' 'LOGIN_REDIRECT_URL =('profile:user_profile',kwargs = {「slug」:self.slug})' – Rahul

+0

@Rahul - 這是一個單獨的問題(您應該提出一個新的問題。 reverse_lazy'但這不會工作,因爲沒有自我 – Sayse

+0

是編寫自定義的'loginview'而不是默認的'auth_views.login'更好的做法 – Rahul