Giter Site home page Giter Site logo

habr_clone's Introduction

Virtual environment

Run from the project root

python3.9 -m venv --copies venv

activate

source venv/bin/activate

and install packages

pip install -r requirements.txt

pip freeze > requirements.txt

PostgreSQL

Create database and user

sudo su postgres
psql
CREATE USER user_1 WITH PASSWORD 'user' CREATEDB;
CREATE DATABASE test_db OWNER user_1;
GRANT ALL PRIVILEGES ON DATABASE test_db TO user_1;
exit

Django

Edit settings.py

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "test_db",
        "USER": "user_1",
        "PASSWORD": "user",
        "HOST": "localhost",
        "PORT": 5432,
    }
}

Superuser

python manage.py createsuperuser --username admin

Run db migrations

python manage.py makemigrations
python manage.py migrate
# check with linter (optional)
python manage.py makemigrations --lint post

Collect static files in app/static

python manage.py collectstatic -c --no-input

Pytest

Run pytest

pip install pytest-django
pip install pytest-cov

pytest -s -v tests/
pytest --cov  --cov-report=html

django.test

Run tests

# Run all the tests
$ python manage.py test 

# Run all the tests found within the 'tests' package
$ python manage.py test tests

# Run all the tests in the tests.test_view_index module
$ python manage.py test tests.test_view_index

# Run just one test case
$ python manage.py test tests.test_view_index.PostIndexView

# Run just one test method
$ python manage.py test tests.test_view_index.PostIndexView.test_no_post
 Example how to сreate multiple instances
 number_of_students = 30
 for student_id in range(number_of_students):
    Student.objects.create(first_name=f"John{student_id}", last_name=f"Doe{student_id}")

Django-taggit 2.1.0

Run django-taggit

pip install django-taggit

Add "taggit" to your INSTALLED_APPS
---------------------------------------
from django.db import models

from taggit.managers import TaggableManager


class Food(models.Model):
    # ... fields here

    tags = TaggableManager()
________________________________________

python manage.py makemigrations
python manage.py migrate
________________________________________

>>> apple = Food.objects.create(name="apple")
>>> apple.tags.add("red", "green", "delicious")
>>> apple.tags.all()
[<Tag: red>, <Tag: green>, <Tag: delicious>]
>>> apple.tags.remove("green")
>>> apple.tags.all()
[<Tag: red>, <Tag: delicious>]
>>> Food.objects.filter(tags__name__in=["red"])
[<Food: apple>, <Food: cherry>]    

Django Taggit Rest Serializer

Installation To install this package you can use the following pip installation:

pip install dj-taggit-serializer

Then, add taggit_serializer to your Settings in INSTALLED_APPS:

INSTALLED_APS = (
    ...
    'taggit_serializer',
)

To accept tags through a REST API call we need to add the following to our Serializer:

from taggit_serializer.serializers import (TagListSerializerField,
                                           TaggitSerializer)


class YourSerializer(TaggitSerializer, serializers.ModelSerializer):

    tags = TagListSerializerField()

    class Meta:
        model = YourModel

And you're done, so now you can add tags to your model

!!!IMPORTANT!!!

  tags in the field enter in the following form ["tag1","tag2"]  , otherwise there will be an error
   -Invalid json list. A tag list submitted in string form must be valid json.-

How to run Docker

delete images, containers, volumes

# Show images, containers, volumes
docker ps -a
docker images
docker volume ls
# Delete images, containers, volumes
docker system prune -a
docker volume prune

Create environment and install packages

# Create environment
python3.8 -m venv --copies app/venv
# Activate
source app/venv/bin/activate
# Make sure to use app/venv/bin/pip3.8 
which pip3.8
# Install packages
pip3.8 install -r app/requirements.txt

Collect static files in app/static

python manage.py collectstatic -c --no-input

Run all at once

docker-compose up -d --build --force-recreate

Run db migrations

docker-compose exec app ./manage.py makemigrations --lint post
docker-compose exec app ./manage.py migrate
docker-compose exec app ./manage.py createsuperuser

Create Category model 'Economy', 'Technology', 'Sport', 'Music'

docker-compose exec app ./manage.py create_category

habr_clone's People

Contributors

arsenajiev avatar s10-lee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.