-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SearchConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'search' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from . import forms | ||
|
||
|
||
def search(request): | ||
form = forms.SearchForm() | ||
return { | ||
'search_form': form, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django import forms | ||
|
||
|
||
class SearchForm(forms.Form): | ||
search = forms.CharField() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
for field in self.fields: | ||
self.fields[field].error_messages = { | ||
'required': 'پر کردن این فیلد ضروری است!', | ||
'invalid': 'این فیلد رو به درستی وارد کن!', | ||
} | ||
# search | ||
search = self.fields['search'] | ||
search.widget.attrs['class'] = 'form-control me-2 search-input rtl' | ||
search.widget.attrs['type'] = 'search' | ||
search.widget.attrs['dir'] = 'auto' | ||
search.widget.attrs['aria-label'] = 'Search' | ||
search.widget.attrs[ | ||
'placeholder' | ||
] = 'جستجو...' | ||
search.error_messages = { | ||
'required': 'پست ات حتما باید عنوان داشته باشه!', | ||
'max_length': 'توضیحات پست ات خیلی زیاد شد!', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
|
||
app_name = 'search' | ||
|
||
urlpatterns = [ | ||
path('', views.SearchView.as_view(), name='search'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.views import generic | ||
|
||
from . import forms | ||
from post.models import Post | ||
|
||
|
||
class SearchView(generic.ListView): | ||
form_class = forms.SearchForm | ||
model = Post | ||
context_object_name = 'posts' | ||
paginate_by = 24 | ||
|
||
def get_queryset(self): | ||
queryset = super().get_queryset() | ||
query = self.request.GET.get('search') | ||
if query: | ||
queryset = queryset.filter( | ||
description__contains=query, | ||
) | ||
return queryset | ||
|
||
def get_template_names(self): | ||
if self.request.headers.get('x-requested-with') == 'XMLHttpRequest': | ||
return 'post/add-posts.html' | ||
return 'post/list.html' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters