GithubHelp home page GithubHelp logo

kennethmiller18 / blog-with-django-3.2.9-full-python-3.8 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielarturoalejoalvarez/blog-with-django-3.2.9-full-python-3.8

0.0 0.0 0.0 1.91 MB

Software of Application with Python

Python 56.79% HTML 43.21%

blog-with-django-3.2.9-full-python-3.8's Introduction

Blog-With-Django-3.2.9-Full-Python-3.8

Description

This repository is a Software of Application with Python.

Virtual

Using pipenv, virtualenv preferably.

Installation

Using Django, Django Rest Framework preferably.

alt text

DataBase

Using SQLite3, PostgreSQL, MySQL, MongoDB,etc.

Apps

Using Postman, Insomnia, Talend API Tester,etc.

Usage

$ git clone https://github.com/DanielArturoAlejoAlvarez/Blog-With-Django-3.2.9-Full-Python-3.8.git [NAME 
$ source env/bin/activate
$ python manage.py makemigrations
$ pthyon manage.py migrate
$ python manage.py runserver

Follow the following steps and you're good to go! Important:

alt text

Coding

Config

DATABASE_URI='mysql+pymysql://{}:{}@{}/{}'.format(DB_USER,DB_PASSWORD,DB_HOST,DB_NAME)

Routes

from posts.views import (
    PostCreateView,
    PostDeleteView,
    PostDetailView,
    PostListView,
    PostUpdateView,
    like
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
    path('', PostListView.as_view(), name='list'),
    path('create/', PostCreateView.as_view(), name='create'),
    path('<slug>/', PostDetailView.as_view(), name='detail'),
    path('<slug>/update/', PostUpdateView.as_view(), name='update'),
    path('<slug>/delete/', PostDeleteView.as_view(), name='delete'),
    path('like/<slug>/', like, name='like'),
]

Models

class User(AbstractUser):
    avatar=models.ImageField(upload_to='users/', height_field=None, width_field=None, max_length=512)

    def __str__(self):
        return self.username

class Post(models.Model):
    title=models.CharField(max_length=100)
    content=models.TextField()
    thumbnail=models.ImageField(upload_to='posts/', height_field=None, width_field=None, max_length=512)
    publish_date=models.DateTimeField(auto_now_add=True)
    last_updated=models.DateTimeField(auto_now=True)
    author=models.ForeignKey(User, on_delete=models.CASCADE)
    slug=models.SlugField()

    def get_absolute_url(self):
        return reverse("detail", kwargs={"slug": self.slug})

    def get_like_url(self):
        return reverse("like", kwargs={"slug": self.slug})

    @property
    def comments(self):
        return self.comment_set.all()

    @property
    def get_comment_count(self):
        return self.comment_set.all().count()

    @property
    def get_like_count(self):
        return self.like_set.all().count()

    @property
    def get_view_count(self):
        return self.postview_set.all().count()

    def __str__(self):
        return self.title


class Comment(models.Model):
    user=models.ForeignKey(User, on_delete=models.CASCADE)
    post=models.ForeignKey(Post, on_delete=models.CASCADE)
    content=models.TextField()
    timestamp=models.DateTimeField(auto_now_add=True)   

    def __str__(self):
        return self.user.username

class PostView(models.Model):
    user=models.ForeignKey(User, on_delete=models.CASCADE)
    post=models.ForeignKey(Post, on_delete=models.CASCADE)
    timestamp=models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.username

class Like(models.Model):
    user=models.ForeignKey(User, on_delete=models.CASCADE)
    post=models.ForeignKey(Post, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.user.username

Views

class PostDetailView(DetailView):
    model=Post

    def post(self, *args, **kwargs):
        form = CommentForm(self.request.POST)
        if form.is_valid():
            post = self.get_object()
            comment = form.instance
            comment.user = self.request.user
            comment.post = post
            comment.save()
            return redirect('detail', slug=post.slug)
        return redirect('detail', slug=self.get_object().slug)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            'form': CommentForm
        })
        return context
    

    def get_object(self, **kwargs):
        obj = super().get_object(**kwargs)
        #if self.request.user.is_authenticated:
        PostView.objects.get_or_create(user=self.request.user, post=obj)
        return obj

class PostCreateView(CreateView):
    form_class=PostForm
    model=Post 

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            'view_type': 'create'
        }) 
        return context


def like(request,slug):
    post=get_object_or_404(Post, slug=slug)
    like_query_set=Like.objects.filter(user=request.user, post=post)
    if like_query_set.exists():
        like_query_set[0].delete()
        return redirect('detail', slug=slug)

    Like.objects.create(user=request.user, post=post)
    return redirect('detail', slug=slug)
        

Forms

class PostForm(forms.ModelForm):
    class Meta:
        model=Post
        fields=('__all__')

class CommentForm(forms.ModelForm):
    content=forms.CharField(required=True, widget=Textarea(attrs={
        'rows': 4
    }))
    class Meta:
        model=Comment
        fields=('content',)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Blog-With-Django-3.2.9-Full-Python-3.8. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.


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.