2014-04-15 19 views
-1

我的表單由於某種原因沒有通過。我對django還是個新手。它應該顯示在每個網址上,因此它位於base.html中,並且它有自己的應用程序;利益。只有按鈕 '添加' 自帶通過,而不是形式.. :(表單不通過

interest.html(包括在base.html文件)

<form method='POST' action=''> {% csrf_token %} 
     <div class='row'> 
     {{ interests_form.interest }} 
     <input class='btn btn-success' type='submit' value='add'/> 
     </div> 
    </form> 

views.py

def user_interest(request): 
    interest = Interests.objects.all() 
    interests_form = InterestsForm(request.POST or None) 

    if interests_form.is_valid(): 
     new_interest = interests_form.save(commit=False) 
     new_interest.save() 
     return HttpResponseRedirect('/') 

    context = locals() 
    return render(request, 'interest.html', context) 

forms.py

class InterestsForm(forms.ModelForm): 
    class Meta: 
     model = Interests 
     widgets = { 
      'interest': forms.TextInput(attrs={'placeholder': 'New Interest'}), 
     } 

models.py

class Interests(models.Model): 
    interest = models.CharField(max_length=100) 
    created = models.DateTimeField(auto_now_add=True) 

urls.py

urlpatterns = patterns('', 
    (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 
     'document_root': settings.STATIC_ROOT }), #needed for profile pic 
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', { 
     'document_root': settings.MEDIA_ROOT }), #needed for profile pic 
    url(r'^admin/', include(admin.site.urls)), 
    (r'^accounts/', include('registration.backends.default.urls')), 
    url(r'^$', 'profiles.views.base', name='home'), 
    url(r'^users/$', 'profiles.views.all', name='users'), 
    url(r'^about/$', 'about.views.about_us', name='about_us'), #the name is not needed in most cases 
    url(r'^members/(?P<username>\w+)/$', 'profiles.views.single_user'), 
    url(r'^edit/$', 'profiles.views.edit_profile', name='edit_profile'), 
    (r'^edit/jobs$', 'profiles.views.edit_jobs'), 
    (r'^edit/locations$', 'profiles.views.edit_locations'), 
    url(r'^posts/$', 'posts.views.user_post', name='posts'), #change the middle section to: posts.views.''' 
    url(r'^search/$', 'posts.views.search', name='search'), 
) 
+1

你還可以顯示你的'urls.py'嗎? –

+0

該網址已添加。我沒有針對興趣應用的urls.py,也沒有針對應用中views.py的視圖。 – Christo

+0

那就是你的問題。 –

回答

1

在urls.py中沒有引用user_interest視圖,也沒有在其他地方使用該視圖。裏面的代碼永遠不會運行。

如果你想添加interest_form的所有網頁,你有這樣做的主要有兩種方式:


例如,如果您選擇要編寫上下文處理器,您可以在應用程序目錄中創建一個context_processors.py文件並編寫這個簡單的處理器:

​​

你會那麼需要把這個函數添加到背景處理器中設置所謂TEMPLATE_CONTEXT_PROCESSORS列表 - 類似your_project.your_app.context_processors.user_interests東西。

這會使interest查詢集和interests_form表單在所有模板中可用。您還需要添加一個單獨的視圖來處理髮送後的表單。

+0

我在哪裏添加單獨的視圖?順便通過現在的形式,非常感謝!現在它只需要工作。 – Christo

+0

好的,想出了視圖。最後一件事:當我提交表單時,表單隨着列出的對象一起消失。我必須訪問另一個網址才能再次顯示它們。我把這個添加到視圖中:interest_form = InterestsForm(request。POST或無) 如果interests_form.is_valid(): new_interest = interests_form.save(提交=假) new_interest.save() 返回HttpResponseRedirect( '/') – Christo

0

嘗試定義您的上下文變量,如下所示:

context = (
'interests_form': interests_form, 
) 

可能也值得嘗試只是:

{{ interests_form.as_p }} 

開始說起,看如果任何東西出現在您呈現的HTML中