-
-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rendering text choices in a UnicornView #324
Comments
Hey, this is a good question! Usually I don't use Django Forms/ModelForms to render the widget HTML so I haven't thought about how <!-- new-course.html -->
<div>
<input unicorn:model="title" type="text" /><br />
<input unicorn:model="grade_level" type="text" /><br />
<input unicorn:model="subject" type="text" /><br />
<button unicorn:click="$validate">Validate</button>
</div> However, if you want the form to be rendered automatically by Django you could do something like this: class NewCourseView(UnicornView):
form_class = NewCourseForm
def form(self):
return self.form_class() {{ form }} However, the above won't be "integrated" with |
Thanks for the quick reply @adamghill! My main thought behind wanting to use django's model form was for its convenience. For example, django will automatically populate a select elements options with the choices defined in the model. As far as a potential implementation goes, one possibility might be to extend Django's different model fields and add a binding or model attribute that would determine what attribute on your UnicornView would be updated.
|
Yep, I gotcha. If you don't use the built-in rendering, you could manually create the component something like this: class NewCourseView(UnicornView):
form_class = NewCourseForm
course = Course()
def form(self):
return self.form_class(instance=self.course)
def save(self):
self.course.save() <select unicorn:model="course.grade_level">
{% for grade_level in form.Meta.model.GradeLevel.choices %}
<option value="{{ grade_level.0 }}">{{ grade_level.1 }}</option>
{% endfor %}
</select>
<!-- other selects here --> Where I'm a little stuck is I could write some JavaScript so that the Django form rendering is integrated with There might be a way to do this elegantly with form widgets attrs (https://docs.djangoproject.com/en/4.0/ref/forms/widgets/#styling-widget-instances) or the new form template in 4.0 (https://docs.djangoproject.com/en/4.0/ref/forms/renderers/#overriding-built-in-form-templates) that I could look into more if you'd like. |
I have a UnicornView that I'm using to display a form with multiple fields that have TextChoices. What's the best way to do this with Unicorn? In a standard django view, I'd use the ModelForm to handle rendering the form field, but I'm not sure that approach would work without requiring reloading the page.
NewCourseView:
NewCourseForm:
Course Model:
The text was updated successfully, but these errors were encountered: