Question

My form does not come through for some reason. I am still new to django. It is supposed to be displayed on every url, so it is located in the base.html, and it have its own app; interest. It is only the button 'add' that comes through, not the form.. :(

interest.html (is included in the 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'),
)
Was it helpful?

Solution

The user_interest view is not referenced in urls.py nor is it used anywhere else. The code inside will never run.

If you want to add the interest_form to all your pages, you have two main ways of doing that:


For example if you chose to write a context processor you would create a context_processors.py file in your app's directory and write this simple processor:

def user_interest():
    interest = Interests.objects.all()
    interests_form = InterestsForm()
    return {
        'interest': interest,
        'interests_form': interests_form,
    }

You would then need to add this function to the list of context processor in your settings called TEMPLATE_CONTEXT_PROCESSORS - something similar to your_project.your_app.context_processors.user_interests.

This would make the interest queryset and interests_form form available in all your templates. You would also need to add a separate view for processing the form after it is sent.

OTHER TIPS

Try defining your context variable as follows:

context = (
'interests_form': interests_form,
)

It may be also worth trying just:

{{ interests_form.as_p }}

to begin with, to see if anything appears in your rendered HTML

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top