Skip to Content

Resume Uploader

Understanding the settings.py File

The settings.py file is a crucial part of any Django project, including a Django Rest Framework project. It contains various configurations that define the behavior and settings of the project. Let's examine the important sections of the settings.py file.

Installed Apps

The INSTALLED_APPS setting lists all the Django applications used in the project. In the case of a Django Rest Framework project, additional applications such as rest_framework and api are included. These applications provide functionalities specific to building RESTful APIs.

INSTALLED_APPS = [    
...
'rest_framework',
'api',
]

Middleware

The MIDDLEWARE setting defines a list of middleware classes that process requests and responses in the project. Each middleware component performs specific functions such as handling sessions, authentication, and more.

URL Configuration

The ROOT_URLCONF setting points to the URL configuration module for the project. In our example, it is set to "resumeuploader.urls," which determines the structure and routing of URLs within the Django Rest Framework project.

Database Configuration

The DATABASES setting holds the configuration details for the project's database connection. In our example, a SQLite database is used, but Django supports various databases, including PostgreSQL, MySQL, and Oracle.

Static Files and Media

The STATIC_URL setting defines the URL prefix for static files, such as CSS and JavaScript. The MEDIA_ROOT and MEDIA_URL settings handle the storage and serving of media files, such as user-uploaded images.

resumeuploader/urls.py 

  • Defining URL Patterns:

    • Set up a URL pattern for the admin site by using the path function with the endpoint "admin/" and the admin.site.urls view.
    • Set up a URL pattern for the API by using the path function with the endpoint "api/" and including the "api.urls" module.
  • Serving Media Files:

    • Include the static function to handle the serving of media files.
    • Use the settings.MEDIA_URL and settings.MEDIA_ROOT variables to define the URL and document root for media files respectively.

api/admin.py

  • Register the "Profile" model with the admin site:

    • Use the @admin.register(Profile) decorator to register the "Profile" model.
    • Create a subclass of admin.ModelAdmin called ProfileModelAdmin to customize the administration options.
  • Define the displayed fields in the admin list view:

    • Set the list_display attribute to specify the fields to be displayed in the admin list view.
    • The listed fields are: 'id', 'name', 'email', 'dob', 'state', 'gender', 'location', 'pimage', 'rdoc', 'phone', 'address', 'summary', 'skills', 'experience', 'education', 'projects', 'achievements', and 'hobbies'.

api/models.py

  • Define the choices for the "state" field:

    • Create a tuple of choices named STATE_CHOICE that represents different states such as "Punjab," "Sindh," "KPK," and "Karachi."
  • Define the "Profile" model:

    • Create a class named Profile that inherits from models.Model.
    • Define various fields to represent different attributes of a profile, such as:
      • name: CharField with a maximum length of 100 characters.
      • email: EmailField with a maximum length of 100 characters.
      • dob: DateTimeField to store the date of birth.
      • state: CharField with choices from the STATE_CHOICE tuple.
      • gender: CharField with a maximum length of 100 characters.
      • location: CharField with a maximum length of 100 characters.
      • pimage: ImageField to upload profile images.
      • rdoc: FileField to upload documents.
      • phone: CharField with a maximum length of 15 characters.
      • address: CharField with a maximum length of 100 characters.
      • summary: TextField with a maximum length of 1000 characters.
      • skills: TextField with a maximum length of 100 characters.
      • experience: TextField with a maximum length of 1000 characters (optional, can be left blank).
      • education: TextField with a maximum length of 1000 characters (required, cannot be left blank).
      • projects: TextField with a maximum length of 100 characters (optional, can be left blank).
      • achievements: TextField with a maximum length of 100 characters (optional, can be left blank).
      • hobbies: TextField with a maximum length of 100 characters (required, cannot be left blank).

api/serializers.py

  • Import the required modules:

    • Import the model module from pyexpat (although it is not used in the code snippet).
    • Import the serializers module from rest_framework.
    • Import the Profile model from the api.models module.
  • Define the ProfileSerializer class:

    • Create a class named ProfileSerializer that inherits from serializers.ModelSerializer.
    • Set the Meta class to specify the model and fields for serialization.
      • The model attribute is set to the Profile model.
      • The fields attribute is set to '__all__', indicating that all fields from the model should be included in the serialized representation.

api/urls.py

Define the URL patterns:

  • Create a list named urlpatterns to hold the URL patterns.
  • Use the path function to define URL patterns.
  • The first pattern is for the "resume" endpoint:
    • Set the URL path to "resume/".
    • Associate the ProfileView class-based view from the views module with the endpoint.
    • Give the URL pattern the name "resume".
  • The second pattern is for the "list" endpoint:
    • Set the URL path to "list".
    • Associate the ProfileView class-based view from the views module with the endpoint.
    • Give the URL pattern the name "list".

api/views.py

Define the ProfileView class, inheriting from APIView:

  • Define the post method to handle POST requests for creating profiles.

    • Create an instance of the ProfileSerializer by passing the request data.
    • Check if the serializer is valid using is_valid().
    • If valid, save the profile using serializer.save().
    • Return a JSON response with a success message, status, and serialized data.
    • If not valid, return a JSON response with serializer errors.
  • Define the get method to handle GET requests for retrieving profiles.

    • Retrieve all profile instances using Profile.objects.all().
    • Serialize the profiles using ProfileSerializer with many=True.
    • Return a JSON response with the serialized data and success status.

GitHub Repository







Rating
0 0

There are no comments for now.

to be the first to leave a comment.