Django form doesn't render

回眸只為那壹抹淺笑 提交于 2019-12-25 08:49:22

问题


Hello and thank you in advance. I have a django form that is not rendering on a template. The "submit" button renders, but the form doesn't. I have been staring at this code for about 6 hours and it is probably something very simple, I probably need another pair of eyes on it.

My Model:

#models.py

from django.db import models
from django.forms import ModelForm

class DraftInput(models.Model):
    player_id = models.IntegerField(help_text = 'Input Player ID Number', max_length = 5)

    def __unicode__(self):
        return self.player_id

class DraftForm(ModelForm):
    class Meta:
        model = DraftInput 

My views:

#views.py

from django.shortcuts import render_to_response

from simple.models import DraftInput
from simple.models import DraftForm

from django.template import RequestContext

#...    

def draft_view(request):

    if request.method == 'POST':
        form = DraftForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = DraftForm()

    return render_to_response('simple/draft_input.html', {'form': form} )

My template:

#draft_input.html

#...

        <h1>Draft Input</h1>
        <h2>Draft</h2><br /><br />
            <form method="POST" action="">
            <table>{{ form }}</table>
            <input type="submit" value="Draft Player" />
        </form><br /><br />
#...

Thank you for your help.


回答1:


Reading the answers thread, it seems you're still out of luck.

Try performing these commands in a django interactive shell (python manage.py shell). It's possible that an exception is thrown when you try to format the form. When an exception is thrown during the form generation in the template, you won't see much in the HTML; but a django shell usually unveils the culprit.

python manage.py shell

In [1]: from simple.models import DraftForm

In [2]: form = DraftForm()

In [3]: form.as_p()

You can either see the HTML generated here, or would catch an exception. This will tell you more about what you need to fix.




回答2:


Either use {{ form.as_table }} or remove the <table></table> from around your form tag.




回答3:


I think that it is a sign to move to generics classes :) Just try CreateView class

In your views.py file:

class CreateDraftInputView(CreateView):
    model = DraftInput
    template_name = "test/draftinput_form.html"
    success_url = "/test/listdraft/"

Simple create template:

<form method="POST" action=".">
    <table>
    {% csrf_token %}
    {{ form.as_table }}
    <input type="submit" />
    </table>
</form>

And very simple list of records in your urls.py:

, (r"^listdraft/$", ListView.as_view(model = DraftInput, template_name = "draftinput_list.html"))



回答4:


Try {{ form.as_p }} for example. There are other shortcuts. You can also manually create the form. Have a look into the Django Docs : https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs




回答5:


Try "form.as_p" or "form.as_table" instead of just "form" in your html file.

EDIT: check out the documentation and use one of their examples as a model: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

In your example, you define form parameters in your model, which may be incorrect. You want to leave form specific modifications, like help_text in the DraftForm class, now in the DraftInput model.

This is a simple issue, you should just replicate something from the docs and then work towards what you want.

Hope this helps.




回答6:


Could not quickly find the root cause, but the fields in my form only show up when the variable declaration follows the conventional module.Class form. The issue might be somewhere in classloading order.

from simple import models

# ...
form = models.DraftForm()


来源:https://stackoverflow.com/questions/9728174/django-form-doesnt-render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!