Comments (3)
I think this is just a limitation of Django's widget implementation.
The name
of a widget is a special attribute which is directly inherited from the field name and cannot be overwritten by other custom attributes. If you have a look at the actual source code (not the code inspection tool), you will see that the name attribute exists twice on your input field and browsers just consider the first name
attribute to be the correct one.
There are also a few questions on StackOverflow about this topic, e.g.:
- Change the name attribute of form field in django template using custom template tags
- Override Django form field's name attr
However, just out of curiosity: What is your use case for this? Why not simply changing the name in the form itself? Keep in mind that you won't be able to submit the input field with the new name, since the form expects the original name in response.
from django-widget-tweaks.
@timoludwig
Thank you for your answer! That pretty much clears it.
However, just out of curiosity: What is your use case for this? Why not simply changing the name in the form itself? Keep in mind that you won't be able to submit the input field with the new name, since the form expects the original name in response.
I was just watching one tutorial and the guy was overriding the field names and labels left and write using bootstrap4 Django package. So, although this was a username
field, he wanted to display email
label with this:
{% bootstrap_label "Email" %}
{% bootstrap_field form.username show_label=False placeholder="Email" %}
Which is pretty bad coding practice if you ask me, but the curiosity got the best of me (well, actually in this case - the worst of me) and I wondered if I could do something similar using crispy-forms and widget-tweaks.
from django-widget-tweaks.
Well, but that's a totally different question 😅
The name
attribute is only visible in the source code and used on the server-side to identify the submitted values.
What you show to your users in the label and placeholder is up to you - of course you can customize that.
The recommended way of changing the label is in the field definition of the form class rather than in the html code, but of course you can just write:
<label for="{{ form.username.id_for_label }}">Email</label>
and the placeholder can e.g. be customized via the render_field
tag:
{% render_field form.username placeholder="Email" %}
as mentioned in the README.
I don't know anything about crispy-forms, but at first sight I think it serves a similar purpose like widget-tweaks, so I don't see a reason to use both at the same time.
from django-widget-tweaks.
Related Issues (20)
- contact form HOT 1
- Is it possible to add is-valid class after validation HOT 3
- Do there any blockers to bump new release? HOT 13
- Test-Suite missing in tarball HOT 1
- How to use Django template variable on attr or set_data HOT 4
- setting size of a field by accessing the model field max_length HOT 1
- accessing variables in render_field tag HOT 2
- setup.py should specify python_requires HOT 1
- Can not find module name in Django installed App HOT 5
- add another filter add_valid_class?
- WIDGET_ERROR_CLASS and WIDGET_REQUIRED_CLASS via settings HOT 1
- removing "required" from form-check-input
- Does it work on FileField? HOT 2
- widget_tweaks causes 'DeprecationWarning: pkg_resources is deprecated as an API' HOT 1
- _pickle.PicklingError: Cannot pickle ResolverMatch. HOT 1
- Releasing a new version with Django 4.2 support HOT 2
- pkg_resources error in django-widget-tweaks after upgrading to Python 3.12 on my distro (Fedora 39) HOT 2
- render_field type date doesn't respect django settings
- Use Django url tag in render_field HOT 1
- [REQUEST] Have an option to use BoundField.render() instead of str(bounded_field)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-widget-tweaks.