Pablo
Nasrullah
Sure
Thanks a lot, as soon as I reach to my laptop I'll send you what I did okay?
sanaz
Hello friends
I have a question for those who work with Django and are familiar with full-text search in PostgreSQL.
def filter_by_search(self, queryset, name, value):
    query_input = SearchQuery(value)
    search_vector = SearchVector('categoryname', 'name', 'tagsname')
    trigram_similarity = TrigramSimilarity('name', value)
    search_rank = SearchRank(search_vector, query_input)
    results = queryset.annotate(
        trigram_similarity=trigram_similarity,
        rank=search_rank).filter(trigram_similarity__gt=0.1).order_by('-rank')
    return results
This is my search code. 
As you can see, I only used the search vector in the search rank, which basically means that the rank field only stores a number during annotation. 
Now, the issue is that every time I search, it returns the same product or record twice, meaning I get duplicate products in the output. But when I manipulate the search vector here and keep only one field (name), it returns just one of that product or record and works correctly. 
It doesn't matter if I add the tags or category name; even if I have two fields instead of three, the output still becomes duplicated for each record. 
For example, when I search for "t-shirt," only one exists, but it returns two. 
What's the issue? Where is my problem? And how is the search vector related to filtering and annotation here since it is only used in the search rank?
chowmin
Hello friends
I have a question for those who work with Django and are familiar with full-text search in PostgreSQL.
def filter_by_search(self, queryset, name, value):
    query_input = SearchQuery(value)
    search_vector = SearchVector('categoryname', 'name', 'tagsname')
    trigram_similarity = TrigramSimilarity('name', value)
    search_rank = SearchRank(search_vector, query_input)
    results = queryset.annotate(
        trigram_similarity=trigram_similarity,
        rank=search_rank).filter(trigram_similarity__gt=0.1).order_by('-rank')
    return results
This is my search code. 
As you can see, I only used the search vector in the search rank, which basically means that the rank field only stores a number during annotation. 
Now, the issue is that every time I search, it returns the same product or record twice, meaning I get duplicate products in the output. But when I manipulate the search vector here and keep only one field (name), it returns just one of that product or record and works correctly. 
It doesn't matter if I add the tags or category name; even if I have two fields instead of three, the output still becomes duplicated for each record. 
For example, when I search for "t-shirt," only one exists, but it returns two. 
What's the issue? Where is my problem? And how is the search vector related to filtering and annotation here since it is only used in the search rank?
Instead of passing multiple fields as separate arguments, combine them into a single,Your issue arises because SearchVector treats each field separately when passed as multiple arguments. Merging them using + and applying .distinct() will fix the duplicate results.
chowmin
search_vector = (
SearchVector('categoryname', weight='A') +
SearchVector('name', weight='B') +
SearchVector('tagsname', weight='C')
)
𝚂 𝙷 𝙰 𝙼 𝙼 𝙰 𝚂
def loginn(request):
if request.method == 'POST':
unm = request.POST.get('unm')
pwd = request.POST.get('pwd')
myuser = authenticate(request ,username=unm,password=pwd)
if myuser is not None:
login(request,myuser)
print("Login Success")
return HttpResponse("Login Success")
else:
print("Login Failed")
return HttpResponse("Wrong credentials")
return render(request,'login.html')
myuser is returning None
bibi
𝚂 𝙷 𝙰 𝙼 𝙼 𝙰 𝚂
Please share more dertails
got the solution.
i was using this way =>
my_user = User(username=unm,password=pwd,email=email)
my_user.save()
~
Hello everyone, is there a library that can block an account if on some attempt at unsuccessful login block the account
Brian
~
Ibrohim
Hello, I have a database, postgres and sqlite, and in my opinion when postgres doesn’t work, should be work sqlite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env.str('PG_DATABASE', default='postgres'),
'USER': env.str('PG_USER', default='postgres'),
'PASSWORD': env.str('PG_PASSWORD', default='postgres'),
'HOST': env.str('DB_HOST', default='db'),
'PORT': env.int('DB_PORT', default=5432),
},
‘extra’: {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Can someone explain why it doesn’t work?
Maxim
Hello, I have a database, postgres and sqlite, and in my opinion when postgres doesn’t work, should be work sqlite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env.str('PG_DATABASE', default='postgres'),
'USER': env.str('PG_USER', default='postgres'),
'PASSWORD': env.str('PG_PASSWORD', default='postgres'),
'HOST': env.str('DB_HOST', default='db'),
'PORT': env.int('DB_PORT', default=5432),
},
‘extra’: {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Can someone explain why it doesn’t work?
check your opinion against the documentation
Ibrohim
cj
Hello, I have a database, postgres and sqlite, and in my opinion when postgres doesn’t work, should be work sqlite
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env.str('PG_DATABASE', default='postgres'),
'USER': env.str('PG_USER', default='postgres'),
'PASSWORD': env.str('PG_PASSWORD', default='postgres'),
'HOST': env.str('DB_HOST', default='db'),
'PORT': env.int('DB_PORT', default=5432),
},
‘extra’: {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Can someone explain why it doesn’t work?
it doesn't work that way... what are you basing your opinion on?
if you want DB redundancy, you should configure it at DBMS layer, Django's DB configuration is not for that
Ibrohim
cj
Pablo
Pablo
I have learnt lots from there. Documentation is great don’t get me wrong but YouTube is also a good place to learn
clouded
Pablo
Because let’s face it, how many people here haven’t watched a “tutorial” on Django here on YouTube?
clouded
Pablo
Probably more than you'd think.
Lmao yeah sure bro, you have never in your life searched up a YouTube video on how a certain tech topic works.
Anyway, Doesn’t make YouTube a bad learning source
clouded
Pablo
clouded
clouded
Yup
Let's leave it at that.
cj
Pablo
cj
cj
Brown
Viewset is a life saver
Charly
cj
cj
YouTube is great to watch kittens videos and listen to music...
to learn things, you'd better use documentation or well rated books, that's all
cj
even Udemy or Coursera or similar sites are not really good sources, because, you know, the same people that make YouTube videos to "teach others" are the same uploading videos to those sites, but yet they are kind of better than YouTube on some certain courses, but still not the greatest source to learn things
supreme
good day sir please which books do you recommend we read?
Lalit
I am using Simple JWT for token authentication and setting the access token and refresh token as HTTP-only cookies upon login in my React app. The tokens are stored in cookies, but when I make an API call from the React app, the cookies are not being sent to the backend.
Mirco
S
Anyone hosted django website on godaddy with mysql??
Doragonsureiyā
Anyone hosted django website on godaddy with mysql??
Adding the word Django to your Off-Topic question doesn't magically turn it to a Django specific question, so delete your question here and move to the @PythonOfftopic group and ask there.
Lalit
I have added CORS and set CORS_ALLOW_CREDENTIALS =TRUE, but the cookies are still not being sent to the backend when making an API call from my React app.
clouded
Lalit
Http only cookies
clouded
Http only cookies
Wait. I don't understand what cookies have to do with CORS.
Maxim
Mirco
clouded
Lalit
How can I check if my access token and refresh token, stored in HttpOnly cookies, are valid?
Georg
helo! Is there a field in DRF like PrimaryKeyRelatedField but with a key for request, and with a structure of object for response?
Georg
I do it using separate serializers (one for request and another for response), but is there a way do it "in one bottle"?
Maxim
Georg
you don't understand. I mean only in DRF, not django
Maxim
Georg
🤦🏻♂️ omagod
Georg
forget
Lalit
I am setting http only cookies like below:
response.set_cookie(
key="accessToken",
value=access_token,
httponly=True,
secure=False,
samesite="Strict"
)
response.set_cookie(
key="refreshToken",
value=refresh_token,
httponly=True,
secure=False,
samesite="Strict"
)
Mirco
Lalit
Lalit
I also changed samesite Strict to None
Mirco
Yes
if the first level domain is the same, u can just link cookies to ".<main-domain> and they will be visible
samesite should be None then
HAJI MASTAN
can anyone suggest me which front end framework will work great with django as i searched alot and found the answer to nextjs but i want to conform here also,
thanks
HAJI MASTAN
clouded
If you need to have the management of the data decoupled from the frontend, for other clients to use it, for instance, you should also consider creating a REST API that can be consumed by both, a frontend framework and different types of clients.
HAJI MASTAN
Certainly, just using Django without Django Restframework and without a separate single-page application frontend framework is the simpler choice. But it's not as shiny as with a SPA framework. The whole page needs to reload when you do something. So, the answer depends on the type of project. If you want data to be reloaded in the background without having to reload the page and that is kind of a frequent requirement of the project, go for SPA, even if it initially is more effort to learn and to set up the project.
Thanks for breaking it down! I get what you mean—sticking with just Django does sound simpler, even if it’s not as flashy as an SPA setup. The page reload thing doesn’t bother me too much for what I’m thinking. Right now, I’ve got two projects going—one’s a school management system and the other’s for a hospital. Honestly, I don’t need SPA for either since they’re pretty straightforward. I was just curious about trying a frontend framework for a few pages, but after hearing you say Django alone can handle it, I’m kinda sold on that too. Keeps things easy, right?
clouded