Project Completed for now

main
Babit Shrestha 6 months ago
parent 4d804d5bea
commit f04119ba02
  1. BIN
      sahara/db.sqlite3
  2. BIN
      sahara/main/__pycache__/forms.cpython-312.pyc
  3. BIN
      sahara/main/__pycache__/views.cpython-312.pyc
  4. 44
      sahara/main/forms.py
  5. 169
      sahara/main/templates/main/register.html
  6. 30
      sahara/main/views.py
  7. BIN
      sahara/static/profile-images/pic1_boIQ2tD.jpg
  8. 3
      sahara/templates/base.html

Binary file not shown.

@ -1,12 +1,8 @@
from django import forms from django import forms
from django.contrib.auth.forms import AuthenticationForm from django.core.exceptions import ValidationError
from .models import User from .models import User
from django import forms
from .models import User, Service
class UserRegistrationForm(forms.ModelForm): class UserRegistrationForm(forms.ModelForm):
# Password and confirm password fields with custom classes
password = forms.CharField( password = forms.CharField(
widget=forms.PasswordInput(attrs={ widget=forms.PasswordInput(attrs={
'class': 'form-control', 'class': 'form-control',
@ -22,7 +18,8 @@ class UserRegistrationForm(forms.ModelForm):
class Meta: class Meta:
model = User model = User
fields = ['email', 'first_name', 'last_name', 'profile', 'price', 'bio', 'service_offered', 'login_profile'] fields = ['email', 'first_name', 'last_name', 'profile', 'price',
'bio', 'service_offered', 'login_profile']
widgets = { widgets = {
'first_name': forms.TextInput(attrs={ 'first_name': forms.TextInput(attrs={
'class': 'form-control', 'class': 'form-control',
@ -43,7 +40,7 @@ class UserRegistrationForm(forms.ModelForm):
'bio': forms.Textarea(attrs={ 'bio': forms.Textarea(attrs={
'class': 'form-control', 'class': 'form-control',
'placeholder': 'Enter your bio', 'placeholder': 'Enter your bio',
'rows': 4, # You can customize the rows and columns as needed 'rows': 4,
}), }),
'price': forms.NumberInput(attrs={ 'price': forms.NumberInput(attrs={
'class': 'form-control', 'class': 'form-control',
@ -57,22 +54,35 @@ class UserRegistrationForm(forms.ModelForm):
}), }),
} }
def clean_email(self):
email = self.cleaned_data.get('email')
if User.objects.filter(email=email).exists():
raise ValidationError('Email is already registered.')
return email
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
password = cleaned_data.get('password') password = cleaned_data.get('password')
confirm_password = cleaned_data.get('confirm_password') confirm_password = cleaned_data.get('confirm_password')
# Password validation
if password:
if len(password) < 8:
raise ValidationError('Password must be at least 8 characters long.')
if not any(char.isdigit() for char in password):
raise ValidationError('Password must contain at least one number.')
if not any(char.isupper() for char in password):
raise ValidationError('Password must contain at least one uppercase letter.')
# Check if passwords match # Check if passwords match
if password != confirm_password: if password and confirm_password and password != confirm_password:
self.add_error('confirm_password', 'Passwords do not match') raise ValidationError('Passwords do not match.')
return cleaned_data return cleaned_data
def save(self, commit=True):
def clean(self): user = super().save(commit=False)
cleaned_data = super().clean() user.set_password(self.cleaned_data['password'])
password = cleaned_data.get('password') if commit:
confirm_password = cleaned_data.get('confirm_password') user.save()
return user
if password and confirm_password and password != confirm_password:
raise forms.ValidationError("Passwords do not match.")

@ -1,133 +1,44 @@
<!-- templates/registration/register.html -->
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<div class="container"> <div class="container mt-5">
<h2>Register</h2> <div class="row justify-content-center">
<form method="post" enctype="multipart/form-data"> <div class="col-md-8">
{% csrf_token %} <div class="card">
<div class="card-header">Register</div>
<div class="form-group mb-3"> <div class="card-body">
<label for="{{ form.first_name.id_for_label }}">First Name</label> {% if messages %}
{{ form.first_name }} {% for message in messages %}
{% if form.first_name.errors %} <div class="alert alert-{{ message.tags }}">
<div class="invalid-feedback"> {{ message }}
{% for error in form.first_name.errors %} </div>
<p>{{ error }}</p> {% endfor %}
{% endfor %} {% endif %}
</div>
{% endif %} <form method="post" enctype="multipart/form-data">
</div> {% csrf_token %}
<div class="form-group mb-3"> {% for field in form %}
<label for="{{ form.last_name.id_for_label }}">Last Name</label> <div class="form-group mb-3">
{{ form.last_name }} <label for="{{ field.id_for_label }}">{{ field.label }}</label>
{% if form.last_name.errors %} {{ field }}
<div class="invalid-feedback"> {% if field.help_text %}
{% for error in form.last_name.errors %} <small class="form-text text-muted">{{ field.help_text }}</small>
<p>{{ error }}</p> {% endif %}
{% endfor %} {% for error in field.errors %}
</div> <div class="invalid-feedback d-block">
{% endif %} {{ error }}
</div> </div>
{% endfor %}
<div class="form-group mb-3"> </div>
<label for="{{ form.email.id_for_label }}">Email</label> {% endfor %}
{{ form.email }}
{% if form.email.errors %} <button type="submit" class="btn btn-primary">Register</button>
<div class="invalid-feedback"> </form>
{% for error in form.email.errors %} </div>
<p>{{ error }}</p> </div>
{% endfor %} </div>
</div> </div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.password.id_for_label }}">Password</label>
{{ form.password }}
{% if form.password.errors %}
<div class="invalid-feedback">
{% for error in form.password.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.confirm_password.id_for_label }}">Confirm Password</label>
{{ form.confirm_password }}
{% if form.confirm_password.errors %}
<div class="invalid-feedback">
{% for error in form.confirm_password.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.profile.id_for_label }}">Profile Image</label>
{{ form.profile }}
{% if form.profile.errors %}
<div class="invalid-feedback">
{% for error in form.profile.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.price.id_for_label }}">Price</label>
{{ form.price }}
{% if form.price.errors %}
<div class="invalid-feedback">
{% for error in form.price.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.bio.id_for_label }}">Bio</label>
{{ form.bio }}
{% if form.bio.errors %}
<div class="invalid-feedback">
{% for error in form.bio.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.service_offered.id_for_label }}">Service Offered</label>
{{ form.service_offered }}
{% if form.service_offered.errors %}
<div class="invalid-feedback">
{% for error in form.service_offered.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<div class="form-group mb-3">
<label for="{{ form.login_profile.id_for_label }}">Login Profile</label>
{{ form.login_profile }}
{% if form.login_profile.errors %}
<div class="invalid-feedback">
{% for error in form.login_profile.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
<p>Already have an account? <a href="{% url 'login' %}">Login here</a>.</p>
</div> </div>
{% endblock %} {% endblock %}

@ -44,28 +44,28 @@ def home(request):
def register(request): def register(request):
if request.user.is_authenticated: if request.user.is_authenticated:
messages.warning(request,"You are already logged in. Please Logout first to register ") messages.warning(request, "You are already logged in.")
return redirect('home') return redirect('home')
if request.method == 'POST' and request.FILES:
form = UserRegistrationForm(request.POST,request.FILES, instance=request.user)
if form.is_valid():
user = form.save(commit=False)
user.set_password(form.cleaned_data['password'])
user.save()
if request.method == 'POST':
form = UserRegistrationForm(request.POST, request.FILES)
if form.is_valid():
try: try:
user.send_verification_email(user) user = form.save()
messages.success(request, 'Registration successful! Please check your email to activate your account.') messages.success(request, 'Registration successful! You can now login.')
return redirect('login')
except Exception as e: except Exception as e:
logger.error(f"Error sending verification email: {e}") logger.error(f"Registration error: {str(e)}")
messages.error(request, e) messages.error(request, 'An error occurred during registration. Please try again.')
else:
return redirect('login') # Display form errors
for field, errors in form.errors.items():
for error in errors:
messages.error(request, f"{field}: {error}")
else: else:
form = UserRegistrationForm() form = UserRegistrationForm()
messages.error(request, 'email already exists')
return render(request, 'main/register.html', {'form': form})
return render(request, 'main/register.html', {'form': form})
def activate_account(request, uidb64, token): def activate_account(request, uidb64, token):
try: try:
uid = urlsafe_base64_decode(uidb64).decode() uid = urlsafe_base64_decode(uidb64).decode()

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 KiB

@ -62,4 +62,5 @@
<footer class="bg-dark text-white text-center py-3"> <footer class="bg-dark text-white text-center py-3">
<p>&copy; 2025 Sahara. All Rights Reserved. | <a href="#" class="text-white">Privacy Policy</a> | <a href="#" class="text-white">Terms of Service</a></p> <p>&copy; 2025 Sahara. All Rights Reserved. | <a href="#" class="text-white">Privacy Policy</a> | <a href="#" class="text-white">Terms of Service</a></p>
</footer> </footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
Loading…
Cancel
Save