Skip to content

Commit a4c8f6a

Browse files
committed
2 parents 89e988f + a6b800e commit a4c8f6a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

db.sqlite3

0 Bytes
Binary file not shown.

docs/extended-user.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,62 @@ class Profile(models.Model):
5353
The model is implemented by creating a new model called `Profile` that holds a One-To-One relationship with the existing `User` Model through a `OneToOneField`. This allows extra information to be associated with a user. This can be customised to suit your project needs.
5454

5555

56+
### The `ProfileForm`
57+
The `Profile` model has it's form called `ProfileForm` that can be used to add profile details. The `ProfileForm` can be found in `apps/users/forms.py`.
58+
```py
59+
# apps/users/forms.py
60+
...
61+
class ProfileForm(forms.ModelForm):
62+
class Meta:
63+
model = Profile
64+
exclude = ('user', 'role', 'avatar',)
65+
66+
def __init__(self, *args, **kwargs):
67+
super(ProfileForm, self).__init__(*args, **kwargs)
68+
69+
for field_name, field in self.fields.items():
70+
self.fields[field_name].widget.attrs['placeholder'] = field.label
71+
self.fields[field_name].widget.attrs['class'] = 'shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500'
72+
self.fields[field_name].widget.attrs['required'] = False
73+
```
74+
75+
- To enable the use of the form, it is sent as a context from the views, and rendered on the template. The `profile` function of `apps/users/views.py` shows how this is done.
76+
```py
77+
# apps/users/views.py
78+
def profile(request):
79+
profile = get_object_or_404(Profile, user=request.user)
80+
if request.method == 'POST':
81+
form = ProfileForm(request.POST, instance=profile)
82+
83+
if form.is_valid():
84+
form.save()
85+
messages.success(request, 'Profile updated successfully')
86+
else:
87+
form = ProfileForm(instance=profile)
88+
89+
context = {
90+
'form': form,
91+
'segment': 'profile',
92+
}
93+
return render(request, 'dashboard/profile.html', context)
94+
```
95+
96+
- This form is used in the `templates/dashboard/profile.html` to allow the update of user data as seen below:
97+
```jinja
98+
<!--templates/dashboard/profile.html line 88-->
99+
{% for field in form %}
100+
<div class="col-span-6 sm:col-span-3">
101+
<label for="first-name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">{{ field.label }}</label>
102+
{{ field }}
103+
</div>
104+
{% endfor %}
105+
```
106+
107+
Visit http://localhost:8000/users/profile to interact with the **Rocket Django** profile form. This can be adapted easily into any page of the route you want for your application.
108+
109+
![Rocket Django - Styled with Tailwind-Flowbite AppSeed - User profile page](https://github.com/app-generator/dummy/assets/57325382/5488a471-2398-4565-aaf1-fbcfa5b9843b)
110+
111+
56112
## Conclusion
57113
Easily store additional user data, implement custom authentication logic, and integrate with third-party services, all while maintaining the security and reliability of Django's built-in user model. Take control of your user management and tailor it to your specific needs with Rocket Django.
58114

0 commit comments

Comments
 (0)