From django urls import path from import views


Download 240.21 Kb.
Pdf ko'rish
Sana10.11.2023
Hajmi240.21 Kb.
#1765202
Bog'liq
app (3)



app/urls.py
from django.urls import path 
from . import views 
urlpatterns=[ 
path(' ',views.PostView.as_view()), 
path('',views.PostDetail.as_view()), 
path('review//', views.AddComments.as_view(), name='add_comments'), 
path('/add_likes/', views.AddLike.as_view(), name='add_likes'), 
path('/del_likes/', views.DelLike.as_view(), name='del_likes') 
urls.py
from django.conf import settings 
from django.conf.urls.static import static 
from django.contrib import admin 
from django.urls import path, include 
urlpatterns = [ 
path('admin/', admin.site.urls), 
path(' ',include('app.urls')) 

if settings.DEBUG: 
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) 
views.py
from django.shortcuts import render, redirect 
from django.views import View 
from .forms import CommentsForm 
from .models import Post,Likes 
class PostView(View): 
def get(self,request): 


posts=Post.objects.all() 
return render(request,'app/index.html',{'post_list':posts}) 
class PostDetail(View): 
def get(self,request,pk): 
posts=Post.objects.get(id=pk) 
return render(request,'app/detail.html',{'post':posts}) 
class AddComments(View): 
def post(self,request,pk): 
#print(request.POST) 
form=CommentsForm(request.POST) 
if form.is_valid(): 
form=form.save(commit=False) 
form.post_id=pk 
form.save() 
return redirect(f'/{pk}',{'comments':form}) 
def get_client_ip(request): 
x_forwarded_for=request.META.get('HTTP_X_FORWARDED_FOR') 
if x_forwarded_for: 
ip=x_forwarded_for.split(',')[0] 
else: 
ip=request.META.get('REMOTE_ADDR') 
return ip 
class AddLike(View): 
def get(self,request,pk): 
ip_client=get_client_ip(request) 
try: 
Likes.objects.get(ip=ip_client,pos_id=pk) 
return redirect(f'/{pk}') 


except: 
new_like=Likes() 
new_like.ip=ip_client 
new_like.pos_id=int(pk) 
new_like.save() 
return redirect(f'/{pk}') 
class DelLike(View): 
def get(self,request,pk): 
ip_client=get_client_ip(request) 
try: 
lik=Likes.objects.get(ip=ip_client) 
lik.delete() 
return redirect(f'/{pk}') 
except: 
return redirect(f'/{pk}') 
index.html
{% load static %} 
 
 
 
Bosh sahifa 
 
 
 
 

Bosh sahifa

 
 

Maqola

 
{% for post in post_list %} 
 


 
 
 
 

 {{ post.title}} > 

 
{{ post.description}}
{{ post.author}}
 
 
 

 
{% endfor %} 
 
 
 
 

Batafsil

 
 

{{ post.title}}

 



 
 
{{ post.description}}
{{ post.author}}
 
 
 
 
 
 
Yaxshi: {{ post.likes_set.all.count }} 
Yaxshi 
Yomon 
 
 

Izoh qoldirish

 
 
 
{% csrf_token %} 
 
 
 
 
 
 
 
 
 


 
 
 
 
 
 
 
 
 
{% for comment in post.comments_set.all %} 
 
 

{{ comment.name }}

 
{{ comment.text_comments }}
 
 
{% endfor %} 
 
Download 240.21 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling