Here is a complete Django CRUD + User Authentication project (Signup, Login, Logout, Create, Read, Update, Delete). This is a very common internship/interview project and good for your portfolio. πŸš€ 1️⃣ Project Structure Your Django project should look like this: myproject/ β”‚ β”œβ”€β”€ myproject/ β”‚ β”œβ”€β”€ settings.py β”‚ β”œβ”€β”€ urls.py β”‚ β”œβ”€β”€ records/ β”‚ β”œβ”€β”€ migrations/ β”‚ β”œβ”€β”€ templates/ β”‚ β”‚ β”œβ”€β”€ records/ β”‚ β”‚ β”‚ β”œβ”€β”€ record_list.html β”‚ β”‚ β”‚ β”œβ”€β”€ record_form.html β”‚ β”‚ β”‚ └── record_confirm_delete.html β”‚ β”‚ └── registration/ β”‚ β”‚ β”œβ”€β”€ login.html β”‚ β”‚ └── signup.html β”‚ β”‚ β”‚ β”œβ”€β”€ models.py β”‚ β”œβ”€β”€ forms.py β”‚ β”œβ”€β”€ views.py β”‚ β”œβ”€β”€ urls.py β”‚ └── manage.py 2 ️⃣ models.py from django.db import models class Record(models.Model): name = models.CharField(max_length=100) email = models.EmailField() phone = models.CharField(max_length=15) city = models.CharField(max_length=100) def __str__(self): return self.name 3 ️⃣ forms.py from django import forms from .models import Record class RecordForm(forms.ModelForm): class Meta: model = Record fields = ['name', 'email', 'phone', 'city'] 4 ️⃣ views.py from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import login from django.contrib.auth.decorators import login_required from .models import Record from .forms import RecordForm # Signup def signup(request): if request.method == "POST": form = UserCreationForm(request.POST) if form.is_valid(): user = form.save() login(request, user) return redirect('record_list') else: form = UserCreationForm() return render(request, 'registration/signup.html', {'form': form}) # READ @login_required def record_list(request): records = Record.objects.all() return render(request, 'records/record_list.html', {'records': records}) # CREATE @login_required def record_create(request): if request.method == "POST": form = RecordForm(request.POST) if form.is_valid(): form.save() return redirect('record_list') else: form = RecordForm() return render(request, 'records/record_form.html', {'form': form}) # UPDATE @login_required def record_update(request, pk): record = get_object_or_404(Record, pk=pk) if request.method == "POST": form = RecordForm(request.POST, instance=record) if form.is_valid(): form.save() return redirect('record_list') else: form = RecordForm(instance=record) return render(request, 'records/record_form.html', {'form': form}) # DELETE @login_required def record_delete(request, pk): record = get_object_or_404(Record, pk=pk) if request.method == "POST": record.delete() return redirect('record_list') return render(request, 'records/record_confirm_delete.html', {'record': record}) 5 ️⃣ records/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.record_list, name='record_list'), path('create/', views.record_create, name='record_create'), path('update//', views.record_update, name='record_update'), path('delete//', views.record_delete, name='record_delete'), path('signup/', views.signup, name='signup'), ] 6 ️⃣ Main urls.py Inside project urls.py from django.contrib import admin from django.urls import path, include from django.contrib.auth import views as auth_views urlpatterns = [ path('admin/', admin.site.urls), path('', include('records.urls')), path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'), path('logout/', auth_views.LogoutView.as_view(), name='logout'), ] 7️ ⃣ settings.py Add this at the bottom: LOGIN_REDIRECT_URL = '/' LOGOUT_REDIRECT_URL = '/login/' LOGIN_URL = '/login/' 8 ️⃣ Templates record_list.html

Records

Add Record Logout {% for record in records %} {% endfor %}
Name Email Age Actions
{{ record.name }} {{ record.email }} {{ record.age }} Edit Delete
record_form.html

Record Form

{% csrf_token %} {{ form.as_p }}
Back record_confirm_delete.html

Delete Record

Are you sure you want to delete {{ record.name }}?

{% csrf_token %}
Cancel signup.html

Signup

{% csrf_token %} {{ form.as_p }}
Login login.html

Login

{% csrf_token %} {{ form.as_p }}
Signup 9️⃣ Run Project Run migrations: python manage.py makemigrations python manage.py migrate Start server: python manage.py runserver Open: http://127.0.0.1:8000 🎯 What This Project Shows in Interviews This project proves you understand: βœ… Django Models βœ… Django Forms βœ… CRUD operations βœ… Authentication system βœ… URL routing βœ… Templates βœ… Login protection This is exactly the type of project recruiters expect from beginners. update in this models.py and forms.py according to it class Record(models.Model): name = models.CharField(max_length=100) email = models.EmailField() phone = models.CharField(max_length=15) city = models.CharField(max_length=100) def __str__(self): return self.name Since you updated the Record model to include phone and city, you also need to update models.py and forms.py accordingly. Here's the correct and clean version. πŸš€ 1️⃣ Updated models.py from django.db import models class Record(models.Model): name = models.CharField(max_length=100) email = models.EmailField() phone = models.CharField(max_length=15) city = models.CharField(max_length=100) def __str__(self): return self.name βœ… Fields in the model: name email phone city 2 ️⃣ Updated forms.py from django import forms from .models import Record class RecordForm(forms.ModelForm): class Meta: model = Record fields = ['name', 'email', 'phone', 'city'] 3 ️⃣ After Updating Model (Important) Run migrations so Django updates the database. python manage.py makemigrations python manage.py migrate 4 ️⃣ Example Form Output (record_form.html) Your form will automatically generate inputs for: Name Email Phone City because of: {{ form.as_p }} 5 ️⃣ Optional (Better Form with Styling) If you want cleaner form fields: class RecordForm(forms.ModelForm): class Meta: model = Record fields = ['name', 'email', 'phone', 'city'] widgets = { 'name': forms.TextInput(attrs={'placeholder': 'Enter Name'}), 'email': forms.EmailInput(attrs={'placeholder': 'Enter Email'}), 'phone': forms.TextInput(attrs={'placeholder': 'Enter Phone'}), 'city': forms.TextInput(attrs={'placeholder': 'Enter City'}), } βœ… Now your CRUD system supports: Name Email Phone City Here is the step-by-step way to create a Django project and app using CMD (Command Prompt). πŸš€ 1️⃣ Open CMD Press: Windows + R Type: cmd Press Enter. 2 ️⃣ Go to the Folder Where You Want the Project Example: cd Desktop or cd Documents 3️ ⃣ Create a Django Project Run: django-admin startproject myproject Now a folder will be created: myproject Go inside the project: cd myproject 4 ️⃣ Create a Django App Now create the app (for example records): python manage.py startapp records Now the structure becomes: myproject/ β”‚ β”œβ”€β”€ manage.py β”œβ”€β”€ myproject/ β”‚ └── records/ The records app folder will contain: records/ β”‚ β”œβ”€β”€ migrations/ β”œβ”€β”€ admin.py β”œβ”€β”€ apps.py β”œβ”€β”€ models.py β”œβ”€β”€ views.py β”œβ”€β”€ tests.py └── __init__.py 5 ️⃣ Register the App Open settings.py Add the app: INSTALLED_APPS = [ 'records', ] 6 ️⃣ Run the Server Start Django server: python manage.py runserver Open browser: http://127.0.0.1:8000 7 ️⃣ Full CMD Commands (Quick Version) cd Desktop django-admin startproject myproject cd myproject python manage.py startapp records python manage.py runserver βœ… Now you have: Project β†’ myproject App β†’ records πŸ”₯ If you want, I can also show you the professional Django folder structure used in companies, which makes projects 10Γ— cleaner and scalable. in this lgout code is not working This page isn’t working If the problem continues, contact the site owner. HTTP ERROR 405 Ah! The HTTP 405 error happens because the LogoutView in Django expects a POST request by default, but you’re probably trying to logout via a GET request by clicking a link. πŸ” Let’s fix it step by step. 1️⃣ Current logout link You likely have something like: Logout This makes a GET request, which causes HTTP 405. 2️⃣ Use POST request for logout (recommended) Update your template:
{% csrf_token %}
βœ… This will send a POST request and Django will log the user out correctly. 3️⃣ Alternative: Allow GET logout (less secure) If you want to logout with a simple link (not recommended for production), modify urls.py: from django.contrib.auth.views import LogoutView path('logout/', LogoutView.as_view(next_page='/'), name='logout', kwargs={'next_page': '/'}), Or in Django β‰₯3.1, you can override the LogoutView: from django.contrib.auth.views import LogoutView class MyLogoutView(LogoutView): http_method_names = ['get', 'post'] Then in urls.py: path('logout/', MyLogoutView.as_view(), name='logout'), ⚠️ Warning: Using GET for logout is less secure (CSRF attack risk). POST is best practice. 4️⃣ Recommended Fix Summary βœ… Change logout link to a form with POST and CSRF token:
{% csrf_token %}
If you want, I can update your entire CRUD + Login + Logout template and navigation so that Login, Signup, Logout all work properly with buttons and links, ready for interviews. Do you want me to do that? now if i want login and logout function based then what will be views.py file and urls.py file code Got it! You want Login and Logout handled via custom function-based views instead of Django’s class-based views. I’ll rewrite your views.py and urls.py for a function-based authentication system that works with your CRUD app. πŸš€ 1️⃣ views.py (Function-Based Authentication + CRUD) from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django.contrib.auth import login, logout, authenticate from django.contrib.auth.decorators import login_required from .models import Record from .forms import RecordForm # ----------------------------- # AUTHENTICATION VIEWS # ----------------------------- def signup_view(request): if request.method == "POST": form = UserCreationForm(request.POST) if form.is_valid(): user = form.save() login(request, user) # automatically login after signup return redirect('record_list') else: form = UserCreationForm() return render(request, 'registration/signup.html', {'form': form}) def login_view(request): if request.method == "POST": form = AuthenticationForm(request, data=request.POST) if form.is_valid(): user = form.get_user() login(request, user) return redirect('record_list') else: form = AuthenticationForm() return render(request, 'registration/login.html', {'form': form}) def logout_view(request): if request.method == "POST": # logout must be POST for security logout(request) return redirect('login') # optional: show confirmation page for logout return render(request, 'registration/logout.html') # ----------------------------- # CRUD VIEWS # ----------------------------- @login_required def record_list(request): records = Record.objects.all() return render(request, 'records/record_list.html', {'records': records}) @login_required def record_create(request): if request.method == "POST": form = RecordForm(request.POST) if form.is_valid(): form.save() return redirect('record_list') else: form = RecordForm() return render(request, 'records/record_form.html', {'form': form}) @login_required def record_update(request, pk): record = get_object_or_404(Record, pk=pk) if request.method == "POST": form = RecordForm(request.POST, instance=record) if form.is_valid(): form.save() return redirect('record_list') else: form = RecordForm(instance=record) return render(request, 'records/record_form.html', {'form': form}) @login_required def record_delete(request, pk): record = get_object_or_404(Record, pk=pk) if request.method == "POST": record.delete() return redirect('record_list') return render(request, 'records/record_confirm_delete.html', {'record': record}) 2️⃣ urls.py (Function-Based Authentication + CRUD) from django.urls import path from . import views urlpatterns = [ # Authentication path('signup/', views.signup_view, name='signup'), path('login/', views.login_view, name='login'), path('logout/', views.logout_view, name='logout'), # CRUD path('', views.record_list, name='record_list'), path('create/', views.record_create, name='record_create'), path('update//', views.record_update, name='record_update'), path('delete//', views.record_delete, name='record_delete'), ] 3️⃣ Templates Changes Logout Button (POST request)
{% csrf_token %}
Login & Signup forms remain the same
{% csrf_token %} {{ form.as_p }}
βœ… Key Points Logout must use POST β†’ prevents CSRF and HTTP 405 errors. login_view uses AuthenticationForm and authenticate(). login_required ensures CRUD pages are protected. After signup or login, user is redirected to record_list. URLs are simple and all function-based. If you want, I can create a full ready-to-run Django project folder structure with: Function-based login/logout/signup CRUD operations phone and city fields in records Fully working HTML templates …so you can just run it and show to recruiters. Do you want me to do that? now i want only user can change crud who added that record then Ah! You want user-specific CRUD, so that each user can only edit/delete the records they added. βœ… This is very common in real apps and highly valued in interviews. Here’s how to implement it step by step. 1️⃣ Update your models.py Add a ForeignKey to the user for each record. from django.db import models from django.contrib.auth.models import User # import user class Record(models.Model): name = models.CharField(max_length=100) email = models.EmailField() phone = models.CharField(max_length=15) city = models.CharField(max_length=100) # NEW: track which user created this record user = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return self.name βœ… Now each record belongs to a specific user. 2️⃣ Update forms.py (No changes needed for user field) We do NOT want users to select user manually, so exclude user: from django import forms from .models import Record class RecordForm(forms.ModelForm): class Meta: model = Record fields = ['name', 'email', 'phone', 'city'] 3️⃣ Update views.py 3.1 Create When saving, assign request.user to the record: @login_required def record_create(request): if request.method == "POST": form = RecordForm(request.POST) if form.is_valid(): record = form.save(commit=False) record.user = request.user # assign the current user record.save() return redirect('record_list') else: form = RecordForm() return render(request, 'records/record_form.html', {'form': form}) 3.2 Read (List only the user’s records) @login_required def record_list(request): records = Record.objects.filter(user=request.user) # only show records added by current user return render(request, 'records/record_list.html', {'records': records}) 3.3 Update / Delete (Only allow if record belongs to user) @login_required def record_update(request, pk): record = get_object_or_404(Record, pk=pk, user=request.user) # user-specific if request.method == "POST": form = RecordForm(request.POST, instance=record) if form.is_valid(): form.save() return redirect('record_list') else: form = RecordForm(instance=record) return render(request, 'records/record_form.html', {'form': form}) @login_required def record_delete(request, pk): record = get_object_or_404(Record, pk=pk, user=request.user) # user-specific if request.method == "POST": record.delete() return redirect('record_list') return render(request, 'records/record_confirm_delete.html', {'record': record}) 4️⃣ Run Migrations After updating models: python manage.py makemigrations python manage.py migrate βœ… What This Does Only the user who created the record can edit/delete it. Users see only their own records in the list. Security is enforced via get_object_or_404(..., user=request.user). 5️⃣ Optional Enhancements Show a β€œYou cannot edit this record” message if a user tries to access another user’s record URL. Add admin access to view all records (for testing). If you want, I can give you a complete ready-to-run Django project with: User signup/login/logout CRUD for user-specific records Phone and city fields Templates ready with Bootstrap UI …so you can just run it and demo a secure, professional app. Do you want me to do that? after running it is asking (env) D:\python demo\myproject>python manage.py makemigrations It is impossible to add a non-nullable field 'user' to record without specifying a default. This is because the database needs something to populate existing rows. Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit and manually define a default value in models.py. Select an option: python manage.py migrate Ah! This is a common Django migration issue. The problem: you added a new non-nullable field (user) to your Record model, but existing rows in the database don’t have a value for it. Django needs a default to fill them. You have two options: Option 1️⃣: Provide a one-off default now (recommended if testing locally) When Django asks: It is impossible to add a non-nullable field 'user'... Select an option: Type 1 and press Enter. Then it will ask you to provide a default value. You can do one of these: Type the ID of an existing user (e.g., 1) Or create a new superuser before running migrations (python manage.py createsuperuser) and use that ID Django will assign that user to all existing records. Option 2️⃣: Make the field nullable temporarily πŸ’‘ Tip: In new projects, always add the user field before creating records, to avoid this migration headache.