#web-development
2 messages · Page 102 of 1
@blissful slate i fixed it bro
nice one @wanton ridge how'd you do it
my host server was wrong lol, it's 127.0.0.1
i thought it might be that. 👍
@devout coral it's showing 3.1.2 now
import os
import sys
def main():
# """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exo_api.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
this is the code with the error
it's pointing to execute_from_command_line and from exc
What are you running when the error pops up?
wdym
it pops up when I do python manage.py runserver
Can you post the error again please.
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'exo_api.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
Here just delete everything inside of manage.py and paste this. It looks like you might have added a space or something somewhere
ok I did
now try to run the server again
@mortal mango I reproduced your issue...
if I use #!/usr/bin/env python3.8 at the top of the script it fails; if run python3.8 test.sh it fails. but if I run python test.sh it works for me. not sure why. This is all using pyenv installed pythons
oh so what do I do?
if I use #!python at the top then it works. 🤷♂️
wdym #!python
how are you running the script
VPS Ubuntu 18.04
I have a file called tests.py that was automatically installed
that one?
or do I create a new file and put #!python in it
@blissful slate
well what are you typing in to make the script go
python manage.py runserver
where do I import views in urls.py?
or where do I import it from
because I'm getting an error there
@blissful slate @devout coral do you guys know?
oh i thought you're running a script here. i dont know django very well
Well the error you are getting now is what? Import problems in views?
I did from exo_api import views
and it says I have an error there
I have 2 folders named exo_api so i'm not sure if that's the problem or not
Where is the file located that you are trying to import from?
Send me a screenshot of your directory tree please
I have the urls.py file inside the exo_api inside the exo_api
the one that's selected right now
should I rename it?
No wait.
Could you send the entire tree. With the manage.py file and the root folder for the project
that's a screenshot I took yesterday so I don't have the folder with the models folder
Ok, so where is the folder that has the models? Inside of the exo_api folder? Or in the root with manage.py
Ok, now the second picture you sent. What folder is that?
and the urls.py file is in the exo_api folder
the second picture is inside the exo_api folder inside the exo_api folder
so the exo_api folder that's seen in the third picture
should I just rename the exo_api folder that's in the third picture
No don’t do that. That will break it.
Give me like 5 minutes. I need to hop on a conference call
oh ok, take your time
so your root folder is called exo_api correct?
Yes
yeah it's called exo_api
Then inside that folder you have another folder with the same name right?
yeah it holds urls.py, settings.py, etc
These are the only files you need in there. It looks like some of the contents of the venv are in yours and your gitignore delete all of that other stuff
yeah those are the ones I have
I have a pycache folder inside that folder
should I keep that or delete it?
Keep it. Granted if you delete it it will pop up. Those contain stuff that python packages use
Cool
do you know what's causing the error?
from django.contrib import admin
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from exo_api import views
urlpatterns = [
path('admin/', admin.site.urls),
path('exo/', views.exoList.as_view()),
]
it should be from users import views
oh ok
from django.contrib import admin
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from users import views
urlpatterns = [
path('admin/', admin.site.urls),
path('exo/', views.exoList.as_view()),
]
oh ok
let me send them
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
do I need to add exo_api? or users to it?
Yes you need to add your custom apps in there
'users.apps.UsersConfig'
oh ok
and whatever other apps you have
I only have 1
so I understand the purpose of jwt tokens and using them for sessions, but unclear about the purpose of refresh tokens
are refresh tokens supposed to be used for refreshing the original session token in case the user's initiated action happened right before expiration?
or is there a different reason?
@mortal mango Did it work?
it says I have to replace it with WebappConfig so I'll do that
sure
same error for some reason
Which error?
wdym
Your users app is not registered in installed apps
from django.apps import AppConfig
class WebappConfig(AppConfig):
name = 'exo_api'
```this is what I have in apps.py
oh ok
So you got it?
it says database 'exo_api' does not exist
and I tried to make migrations but it brings up another error
How did you set this project up? Seems like it was not set up properly
I uploaded the files from my computer. Do I need to actually startproject?
It works on your computer?
no I never tested it
...
is there a way I can set the project up correctly?
Yes. Get off the server. Start from the beginning on your computer and do not push the files until AFTER you have tested it.
k
in the database section in settings.py, what do I put for the name part?
would I put users?
@devout coral
how do I reset my postgresql password?
@mortal mango What do you mean?
I can't seem to login to the database with the code
it says password authentication failed for user
On your test environment I suggest you use sqllite
When you run migrations it will do it
oh ok
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
That should be somewhere in your settings.py
django.db.migrations.exceptions.NodeNotFoundError: Migration users.0002_auto_20201019_1816 dependencies reference nonexistent parent node ('webapp', '0001_initial')
Oh man. Is this your first django project?
Alright. I suggest a tutorial or something first.
Ok, because you are running into some really silly errors no offense.
You are not
Just easier if you follow a tutorial to learn the basics then me explain all the errors
Do you like video tutorials?
If so follow that tutorial and you will learn a lot.
@mortal mango Nice
I watched a couple minutes of the databases and migrations video and found out how to do it
I thought you started from scratch...
and then I did makemigrations and it worked
Yeah
yeah I started yesterday and then I stopped and continued today
yeah no more
it finally worked!!
so would I do a cname for a subdomain to point to the vps or would I do an a record in the dns
I would suggest an A record
make sure you also set up your settings.py file to allow the host
Ok first official Django project, its a clothing store, 3 categories, so I have to create a Product model, associate this model to one of the three categories, associate a name, price, description, image and etc all to this product model.
Why is this not working in django (its a template html-file)
{% for comment in comments%}
<p id="comment_style">{{comment.text_content}} {{ comment.author }}</p>
<p id="comment_style">{{ commment.author }}</p>
It cant access comment.author the second time I want to use it
The first p-tag is working as intended and shows all the info
But it somehow works if I use like a h6 tag (and probly any other tag)
What is the error you are getting?
Can anyone offer some help with celery and django?
Guys. To run uWsgi with Flask, do I need to setup a virtual env ? I do have a Raspberry and my uWSGI seems to be unable to import libraries 🙂
I have a noob question, what would I use JS for when creating a Flask app
frontend
But css and html are there for it
there are things you can only do with JS, such as infinite scroll
@devout coral hello again, I got the webserver and everything set up but it says forbidden You don't have permission to access this resource. when I try going to the page.
do you know why this happens?
You need to give proper perms to whatever is running your server
yeah I did sudo chown -R :www-data exo_api to give apache access to all of my files
@devout coral
One more thing, is incorporating JS into html files made with templates (in Flask) the same as with Normal html?
Not sure atm and I am leaving the office now. Sorry hope someone else can help.
oh ok
One more thing, is incorporating JS into html files made with templates (in Flask) the same as with Normal html?
yeah, it can work pretty much the same. I have used the<script>tag in django templates a few times and it did its job, this should be the same in flask.
I'm using django and I got an apache webserver and everything set up but it says forbidden You don't have permission to access this resource. when I try going to the page. Does anyone know why this happens?
yeah I have my domain and my vps ip address
hmmmm, this is usually an issue with allowed hosts. This may be an actual server permission error chmod
someone told me it's because apache can't access the files
so what should I do with chmod?
yeah, you need to give apache permission, one sec
I gave apache permission with chown
were you following a guide or tutorial?
yeah
In this Python Django Tutorial, we will be learning how to deploy our application to a Linux Server from scratch using Linode.
If you would like $20 of free credit towards a Linode account, then you can use my link here to sign up:
https://linode.com/coreyschafer
We will be...
I was following this
did you follow the whole series?
you may want to try the permissions again then
sudo service apache2 reload
I didn't follow the whole series. I just watched that video to know how to deploy
I did follow the whole thing 😄
oh ok
anyone know if you can modify tags within a div using internal css?
I can modify the general section but cant seem to figure out how to modify each tag within specifically
@acoustic oyster in the tutorial it says to do something with the media files. I didn't do that because I don't have the media files. Is that fine?
I just reloaded it and it still says forbidden
is it forbidden on every page?
you may need to check your virtual host as well
yeah it's forbidden on every page
oh, double check you have the correct file paths in virtual host AND when you chmod, my project structure was a little different so I had some issues as well.
if these common solutions do not help, then you should check your apache error logs to see what exactly is happening.
well, follow the tutorial for chmod, but double check you are FORSURE doing the correct file paths.
And if you are doing the apache tutorial, you are probably using virtual hosts.
so, like cd /etc/apache2/sites-available and make sure your .conf is setup properly. Again QUADRUPLE check the file paths match your project
my partner was trying to run my django project but cant run from . import views, any idea why? I think the files are in their apprpriate folders
its from putyourapphere import views
my partner was trying to run my django project but cant run
from . import views, any idea why? I think the files are in their apprpriate folders
@plucky tapir what's the error
and where are you importing to
@acoustic oyster so where would I check the file paths? Would it be the aliases that I added?
@acoustic oyster I don't have an html file. I'm using Django Rest Framework to build an API. Does it cause problems if I don't have an index.html file?
@vestal hound
@plucky tapir that means that you're running code from within projects itself
@vestal hound which is fine?
@vestal hound which is fine?
@plucky tapir not if you want to use it as a package.
for the purpose of relative imports
projects is only a package from the outside.
so if, from the root directory, you ran from projects import views, for example, that would work.
and if in projects you had a from . import views, that would work too
unless you were running it from that directory itself, in which case it wouldn't "know" that it was a package, therefore there'd be no parent (.) to import from
@acoustic oyster I don't have an html file. I'm using Django Rest Framework to build an API. Does it cause problems if I don't have an index.html file?
You need to double check how your project is structured and that you included the correct files in your virtual host setup in/etc/apache2/sites-available(is where I think it should be.
You would need to double check that your setup matches the tutorial setup
outside the projects folder?
@plucky tapir how are you running it?
from terminal?
what's your PYTHONPATH?
through terminal and pycharm it runs fine for me. I'm not sure what setup or IDE they're using tbh. And I'll have to look that up real quick
@vestal hound and one small question for user to link to database info, its currenlty linked to user, should it be to the user ID?
through terminal and pycharm it runs fine for me. I'm not sure what setup or IDE they're using tbh. And I'll have to look that up real quick
@plucky tapir pretty sure it's a path issue.
if you run code from within a package relative imports won't work because there's no parent package to import from.
and that's basically the only case in which you get that error.
@vestal hound and one small question for user to link to database info, its currenlty linked to user, should it be to the user ID?
@plucky tapir could you elaborate? I'm not really sure what you mean
appreciate it.
So I'll probably have to try to get their paths in order. I didn't upload my virtual environment since it was too large, is that have anything to do with setup
Sure, right now in Django a user logs in, they use create views and input data. the models.py takes in the user as an author automatically to know who created that in the database
so takes in user as a foreign key
should it take in the user id instead of the user? For good practice
should it take in the user id instead of the user? For good practice
@plucky tapir do you mean in terms of how the related field is defined in theModel?
appreciate it.
So I'll probably have to try to get their paths in order. I didn't upload my virtual environment since it was too large, is that have anything to do with setupSure, right now in Django a user logs in, they use create views and input data. the models.py takes in the user as an author automatically to know who created that in the database
@plucky tapir perhaps. it should just work out of the box though, which makes me think they're not running it properly
like python manage.py runserver should not run into any problems
@vestal hound yep I believe so, right now its author = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE, default=None)
yup
so ForeignKey abstracts away the idea of ID/primary key for you
but internally what happens is that it creates an author_id field and sets up the necessary FK constraints
so in fact, you'll notice that on the model you can access both author (User object) and author_id (int).
Oh wow okay I'll have to try that out to see. Itd be django acceptable to leave it as User ?
Oh wow okay I'll have to try that out to see. Itd be django acceptable to leave it as User ?
@plucky tapir not just acceptable, but preferred
although I question why your FK is nullable
not wrong but I'm curious
@vestal hound alright nice.
@vestal hound Tbh I had some issues at the time with the foreign key, I read through some forums and found someone added blank and null. So still learning but if it’s more conventional to switch it up I’m for it.
@vestal hound alright nice.
@vestal hound Tbh I had some issues at the time with the foreign key, I read through some forums and found someone added blank and null. So still learning but if it’s more conventional to switch it up I’m for it.
@plucky tapir IMO don't use nullable FKs unless you know what you're doing
there's a fair bit of theory behind this
which you can get into if you're interested (it'll help you design better schemata)
but in this specific context...would it make sense to have an instance of whatever model that is without a creator?
@vestal hound I could learn if it’d help me out more down the line.
For now I believe so
the user is inputting info that is specific to them
@app.before_request
def before_request():
database.connect()
@app.after_request
def after_request(response):
database.close()
return response
By default, all your application needs to do is ensure that connections are closed when you are finished with them, and they will be returned to the pool. For web applications, this typically means that at the beginning of a request, you will open a connection, and when you return a response, you will close the connection.
I got this from peewee's documentation. Isn't it a bad idea to recycle a single connection per request? What if you're doing a lot of async stuff? Wouldn't it be better to pull a new connection from the connection pool everytime you do a query. Immediately after you're done you would close the connection so it goes back into the pool
WIth the above solution, each request will have a single connection to play with. The bad side about this is if you have a number of async db calls, all but one is going to be blocked
Anyone ;(. Every thing I've researched suggests that this is a bad idea
Oh wait there is a db channel
anyone have opinions on web frameworks? Looking for things to learn besides Flask
I've been hearing good things about FastAPI, but I had a terrible experience with Connexion so I'm a little hesitant about things that are new
I am heavily biased towards django always xD but django is 
Django is good if you want a quick admin system(customize admin is hell though, not recommend), FastAPI exactly what it's name is to make API super fast(i also love the force typing in api though, from someone who worked with typescript). If you want to do from scrap Flask not a bad choice
in the end use what you need, not which is better
@gaunt marlin most of the jobs are with front end / react if you have the free time to pick up additional frameworks go with react imo
If you can get good with vanilla css and efficient in react you're solid
@pine yew
but those are frontend framework, you always need backend frameworks for server. 2 Different stuffs
Sorry I didn't mean to cc you I thought you were the one asking. Also, yeah I understand, however I think he just asked about a framework not a backend or front one specifically. I'm sure he's interested in backend being as this is a python discord it's just that personally I feel anyone with free time to pick up a framework who wants work, should learn something like react. Plus he said he already knows Flask. I'm a Django guy myself but I just see where the trends are for work etc. Personally you can do a lot more with django and if you already know flask, django may be the best option since there will be less of a learning curve and django can do more for you out of the box. So perhaps my ultimate suggestion would be to become as profficient as you possibly can without stopping with django. In the mean time don't look at any other frameworks unless you're already in the field then I'll stop right there
What is the best way to handle object level permissions in django? hit a bit of a road block on this one.
or, better suited, row level permissions
say I have a table full of quotations, with 1 column being customer
and each "user" is attached to a customer
user being foreign key to a customer in a customer table
how can I ensure each user can only see rows in the quotation table, for which the foreign key is the same customer as his user.customer is?
Do you guys know some good servers for help with Pyramid Framework?
What is the best way to handle object level permissions in django? hit a bit of a road block on this one.
@idle flame vanilla Django?
I never understood how Django works, so I'm going with Pyramid. Can anyone recommend me some Pyramid servers?
@vestal hound yes, i intend putting DRF ontop of it
but pretty much vanilla
still in early development so I can certainly rework something else into it. I've googled the issue to little avail, it seems postgres has something I can integrate to assist but im running on mysql
django guardian but there seems to be some concern it's not applicable to my use case
@vestal hound yes, i intend putting DRF ontop of it
@idle flame okay it depends on whether that's going to be a general thing or specific to your view
if the latter, then your filter should be in the queryset
drf mentions object level permissions but the implementation but that only applies to api calls right?
drf mentions object level permissions but the implementation but that only applies to api calls right?
@idle flame basically, yes.
but where else would you perform that check?
like I believe for that case you want Quotation.objects.filter(customer=request.user.customer)?
bare with me, I'm self teaching here and seem to have hit the limits of my basic understanding. so I'm not running an API call when on the website - only when I'm using the api itself (i'm using it for sending sensor readings into the database)
I also dumbed the query down a little bit - so I have user.organisation (the organisation the user belongs too)
but then on the other side of the query I have organisation.location.area.zone.asset.quotation
and other tables than quotation,
exactly 🙂
okay, but that's all irrelevant
the thrust of the issue is that you only need to impose control at one point: when your API returns data
correct?
its actually the opposite 😦
why?
the only thing using the API is embedded software i've designed
so a little data collector pushing sensor readings to a database
then we analyse the data, make reports in a "report" table
and there are reports many to one asset
of which asset many to one zone, then area, then location then organisation
does it make sense :S
and users will need permissions for different levels of that hieracy
I mean, all that makes sense
has anyone ever used django rest framework? i'm having trouble using nested serializers. i can't get the nested models to show on my api view
has anyone ever used django rest framework? i'm having trouble using nested serializers. i can't get the nested models to show on my api view
@surreal harness what's your specific problem
I mean, all that makes sense
@vestal hound but ultimately, either your API does the permission checking or your embedded software does, right?
i have two models, User and UserContactInformation
UserContactInformation has foreignkey(user)
and i want to nest the contactinformation in the user model in the serializer
so when i go to /api/users/1 it will show the user attributes and the nested contact information
so what I'm ultimately trying to achieve is: users cannot see or interact with assets from other users.
For my use case you recommend applying it via the queryset?
so what I'm ultimately trying to achieve is: users cannot see or interact with assets from other users.
For my use case you recommend applying it via the queryset?
@idle flame yes. why would the embedded software know about such things?
which is why I'm saying you should control access at one point: your API
class UserContactInformationSerializer(serializers.ModelSerializer):
class Meta:
model = UserContactInformation
fields = ['id', 'user', 'contact_information']
class UserSerializer(serializers.ModelSerializer):
user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True)
class Meta:
model = User
fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']
apparently this is supposed to be enough to get it to show properly nested, but it just won't show the nested contact information
the embedded software doesnt know or care, no permissions required. it only knows the asset_id
the embedded software doesnt know or care, no permissions required. it only knows the asset_id
@idle flame precisely, right? so it should let the API handle it.
yes, the API will handle it.
class UserContactInformationSerializer(serializers.ModelSerializer): class Meta: model = UserContactInformation fields = ['id', 'user', 'contact_information'] class UserSerializer(serializers.ModelSerializer): user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True) class Meta: model = User fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']apparently this is supposed to be enough to get it to show properly nested, but it just won't show the nested contact information
@surreal harness what do you mean "won't show"?
what output do you get
BUT the user goes to the website and views the reports
BUT the user goes to the website and views the reports
@idle flame okay, so the embedded software has to know which user is using it
no -
I only get first_name, last_name, and id without even seeing a field for user_contacT_information.
},
{
"id": 4,
"last_name": null,
"first_name": "test user",
}
}
the software sends the data in, I parse it and save it in the table on the back end side
data comes in via API, back end processes it and stores it in a table
the software sends the data in, I parse it and save it in the table on the back end side
@idle flame if it doesn't then it can't perform permission checking
because ultimately the backend has to know which user is requesting data
django has to permission check
to do that, right?
and for the backend to know, the previous layer has to know, too
Sorry - i'm a beginner here - the embedded for all purposes is a superuser
it handshakes and sends the data in via api. django then reads and processes this data, then stores it in the relevent table
the user logs into the front end of website and views this table
i need to restrict the user to only objects in this table that belong too him
because there are hundreds of embedded components
on 20 customers
in 100 different locations
i need to restrict the user to only objects in this table that belong too him
@idle flame yup, I understand that
but the thing is
the user logs into the front end of website and views this table
@idle flame you can't have validation purely on the frontend.
the backend has to know which user is requesting data
otherwise the only choice is to send all the data.
ohh yes -
it does!
the default django user model
I extended this
well i one to one'd a user profile
so each request has to know the source user
yeah, those are details
but the main thing is that if each request is tied to a user
then the problem is trivial
so when user makes a request, I look at this query, crawl around the tables linked to his to see where is view permissions end
then crawl the table he is trying to view and pull only things he has permission for
Why don't image show up on my Django app
okay. I know this is a really dumb question but where is that applied?
so when user makes a request, I look at this query, crawl around the tables linked to his to see where is view permissions end
@idle flame in what form are the user's credentials transmitted?
okay. I know this is a really dumb question but where is that applied?
@idle flame not a dumb question, but where is what applied?
its currently NOT that's what I'm trying to build - sorry beginner here. i wanted it to work via @ login required
and extend this to the user
so he needs to send his user to django and then I need to apply it from this on every page
yup
precisely!
so like
with Django REST framework
each view takes a request, right
and if you set up your authentication backend properly
yes
you can just access request.user
and that will be a User object representing the user asking for data.
then you can just query the Quotation table with the filter I mentioned above
Quotation.objects.filter(customer=request.user.customer)
at the moment I have only the django base authentication back end and I dont know how to extend it and apply it
a good book to learn django please ??
i'm alot further forward so far, sorry @vestal hound this is alot more helpful than it seems. So basically I need to google "extending django authentication back end" instead of "how to enforce object level permission django??"
i'm alot further forward so far, sorry @vestal hound this is alot more helpful than it seems. So basically I need to google "extending django authentication back end" instead of "how to enforce object level permission django??"
@idle flame DRF comes with several authentication backends
you can look into those and see if they fit your usecase
so that's one part of the problem
once you handle that, the rest should be easy
because it'll take care of populating the user attribute of the request object
that's one part.
but do you understand
what I mean by this?
but that will only apply to API calls right? not
then you can just query the
Quotationtable with the filter I mentioned above
@vestal hound this
wait
but that will only apply to API calls right? not
@idle flame yes, but the backend only interacts with the outside through API calls, right
a good book to learn django please ??
@cosmic aspen try looking at this book called two scoops of django
I could change my front end to route via the API though
@idle flame yes, but that's a separate layer
the main point is that the data ultimately comes from the backend
but DRF sits ontop of the back end though? doesnt a standard web browser going to the website
avoid this
so it goes website ->drf -> backend??
DRF is the backend
also, I think you might be a bit confused regarding object-level permissions
basically, normal permissions: "can this user access this view?"
object-level permissions: "this user can access this view, but can they also access this object?"
which is kind of neither here nor there, but just a point
I'm worried that if I make the view it's going to show them everything
(which it is right now)
because object-level permissions are more something you do when you want to get a single object
that is the bit I'm trying to fix
in this case it's a question more of filtering a number of results
okay, I think I need to work DRF in and read it's docs
do you mind if I work on this for a while and message you later?
if you have time @vestal hound can you help me with the issue i was talking about above
you can just ask here and whomever will respond
if you have time @vestal hound can you help me with the issue i was talking about above
@surreal harness wups sorry I entirely forgot
my bad
class UserContactInformationSerializer(serializers.ModelSerializer):
class Meta:
model = UserContactInformation
fields = ['id', 'user', 'contact_information']
class UserSerializer(serializers.ModelSerializer):
user_contact_information_set = UserContactInformationSerializer(many=True, read_only=True)
class Meta:
model = User
fields = ['id', 'first_name', 'last_name', 'user_contact_information_set']
results are just
},
{
"id": 4,
"last_name": null,
"first_name": "test user",
}
}
can I see your models
yeah
User and UserContactInformation
well okay, I sort of reduced the complexity to make it easier to write out
the actual model is like
and also your view
class CandidateContact(models.Model):
class Meta():
db_table = 'candidate_contact'
candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
contact = models.ForeignKey(
ContactType, null=True, on_delete=models.SET_NULL)
contact_detail = models.CharField(max_length=255)
class Candidate(models.Model):
# TODO: Confirm usage of email/phone with the mobile developers as identifier, as opposed to UUID
class Meta():
db_table = 'candidate'
id = models.AutoField(primary_key=True)
last_name = models.CharField(max_length=255, blank=True, null=True)
first_name = models.CharField(max_length=255)
gender = models.ForeignKey(
Gender, default=3, null=True, on_delete=models.SET_DEFAULT)
country = models.ForeignKey(
Country, verbose_name="nationality", null=True, on_delete=models.SET_NULL)
class CandidateContactSerializer(serializers.ModelSerializer):
class Meta:
model = CandidateContact
fields = [
'id',
'candidate',
'contact_detail',
'contact'
]
class CandidateSerializer(serializers.ModelSerializer):
# Retrieve connected fields
candidate_contacts = CandidateContactSerializer(
many=True,
read_only=True,
)
class Meta:
model = Candidate
fields = [
'id',
'last_name',
'first_name',
'gender',
'country',
'candidate_contacts',
]
@api_view(['GET', ])
def candidate_detail_view(request, id):
try:
candidate = Candidate.objects.get(id=id)
except Candidate.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == "GET":
serializer = CandidateSerializer(candidate)
return Response(serializer.data)
# Try class based view
class CandidateListView(ListAPIView):
queryset = Candidate.objects.all()
serializer_class = CandidateSerializer
# authentication_classes = (TokenAuthentication)
# permission_classes = (IsAuthenticated)
pagination_class = PageNumberPagination
I suppose Candidate corresponds to User?
no, its a database filled with some master data
a bunch of people previously registered
to some other service
no, I mean, in your example above
oh yes
this is the view on /api/candidate/1
HTTP 200 OK
Allow: OPTIONS, GET
Content-Type: application/json
Vary: Accept
{
"id": 1,
"last_name": null,
"first_name": "Võ Minh Luân",
"gender": 3,
"country": 235,
"education_in_japan": false
}
as you can see, candidate contact isn't showing up at all. i'm wondering if its because i'm not defining the relationship correctly
that's your function-based view, right?
weird my internet isn't working at all
I want to Google something but I can't
sec
ok
has anyone used python-social-auth with django?
ok
@surreal harness okay this shouldn't be happening
try deleting all your .pyc files?
that's my best guess
there's only one CandidateSerializer, right
like you're not importing one that doesn't have that field
nope
as in yes, there's only one
i have a feeling that
something is messed up with the relation
it looks fine to me
so we know that CandidateContact uses Candidate as a foreign key
although
but is the serializer doing that
should it matter? the database exists
because Candidate doesn't exist when CandidateContact is defined
class CandidateContact(models.Model):
class Meta():
db_table = 'candidate_contact'
candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
purely as a matter of Python
at this point, Candidate is not yet defined, right
like this shouldn't cause that problem, but just a point
right true, well in the models Candidate is defined first
anyway, that is more or less how you should define a nested field, so I don't think it's that
in the serializers CandidateContact needs to be defined first since Candidate depends on it, I think
Django, API, REST, Serializer relations
yeah I followed this guide exactly, but didn't seem to work
came across something that said that query_sets retrieved by the serializers have a default naming convention of having the word set appended to them
however even when adding the word set, as in 'candidate_contact_set' and having the fields include 'candidate_contact_set' instead of just 'candidate_contact', same result
came across something that said that query_sets retrieved by the serializers have a default naming convention of having the word
setappended to them
@surreal harness this is true
but it shouldn't matter
interestingly enough applying depth=1 inside of the Meta class for the Candidate model automatically nests stuff like gender, etc.
I'm actually not sure how this works because I always set custom related_name
I see
let me try
class CandidateSerializer(serializers.ModelSerializer):
# Retrieve connected fields
candidate_contacts = CandidateContactSerializer(
many=True,
read_only=True,
)
class Meta:
model = Candidate
fields = [
'id',
'last_name',
'first_name',
'gender',
'country',
'candidate_contacts',
]
is what it is currently
okay can you try this
in CandidateContact.candidate, set related_name to "candidate_contacts"
so like models.ForeignKey(Candidate, related_name='candidate_contacts', on_delete=models.CASCADE)
this is my best guess
does that require a migration
ah it does. give me a second
does it being plural actually make a difference
my thought process is
if the attribute name is wrong, it can't find the attribute representing the related objects, therefore it assumes that it's null
does it work
about to test
i just have to deploy it and shit, it takes a second
yeah, that worked
okay
so I guess my mental model was accurate
your serializer field name needs to correspond to the related object manager
can flask be used for front end web dev
If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.map_set.all().
this is what i see, however i distinctly remember trying this
let me try again
k
yeah, so when trying to add another relation it seems like this thing they say is actually not true
i have some other related fields and simply appending 'set' to the field name doesn't do anything
uh
I am actually not sure about this, because like I said, I always use custom related_name
which IMO is a good practice
okay, i just don't really know what it represents conceptually
okay
hello
so like
candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
each ContactInfo has a Candidate
how can i log in to my robot
which you can access with, say, contact_info.candidate
but since it's a foreign key (one to many relationship)
yep
each Candidate has many ContactInfos
so candidate.candidate_contacts.all() is the QuerySet of ContactInfos corresponding to that Candidate
where candidate_contacts is the related_name you set
is candidate.candidate_contacts.all() even possible
right, but from the candidate model perspective no relationship is set
that only exists in the contactinfo model
it's more or less the same as ContactInfo.objects.filter(id=candidate_id)
no
it is
i see
ok, got it
the thing is
candidate.candidate_contacts.all()
that candidate_contacts part, that name corresponds to what exactly
the related_name
RIGHT
if you change related_name to monkeys it'll be candidate.monkeys.all()
ok now i really get it
oh ididn't see th elast sentence of what you wrote is why
and basically if I never set that name it should default to something
which i assumed was just the model name defined with underscores and appended with 'set'
but i guess that's not true
but now i know how to check
what happens i think is that
if you define a model like
camel case, it will not apply underscores
so it would just be candidatecontactinformation_set
for models
in this case it should be contactinfo_set
is Flask or Django more beginner friendly?
Flask is beginner friendly.
I had a question guys, how do you know the user in Pyramid?
guys what is good module to work with pdf files in flask? Like to render them??
and as well can someone tell me where can i find good tutorial to save pdf files and images i want thme stored in database
anybody knows fcm django ?
@fickle fox PDF and image are file so you only need to handle store file to database here a good tutorial https://www.youtube.com/watch?v=TLgVEBuQURA&ab_channel=PrettyPrinted
Uploading files in Flask to a database is very simple to accomplish. In this video, I'll show you to use an HTML upload form to upload files to Flask and save them to blob columns in your database using SQLAlchemy. The example database I'm using is SQLite, but most popular dat...
oh thanks
i am following that guide
tho i found some errors in my code qwq
oh never mind i fixed it hahahah
how could i implement a count_only query parameter in the rest framework that would only count the total number of results without paginating them?
@surreal harness use .count() at the end of the query it will return the number instead
in your view query
hm...looks like that might be incompatible with the pagination class
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
```I'm getting internet server error when I try running my django app with apache2. Does anyone know why?
This is the error in the error log:
hi i want to make a game backend using websockts, should i use Hypercorn or what for it? basicly i just want 1 singleton for game and to pass data and close connections when needed
does Hypercorn run on arm, am asking cuz i read there is meybe some cython stuff
ok guys i managed to save image to database but now how can i display it on website
Guys. I'm fine to run my Flask app only with Werkzeug if I'll get only requests from the same computer at most 20 times a day ? I have read that it's able only to hold 1 request at once, my flask doesn't have any static files because it's an API
I've been working on a django restframework project with the aim of generating json files out of my model objects like the following :
“label”: <label>},...]
```
here's the models implementation
```py
from django.db import models
#Document Model
class Document(models.Model):
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.text[:50]
#Label Model
class Label(models.Model):
value = models.CharField(max_length=100)
background_color = models.CharField(max_length=7, default='#209cee')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.value
class SequenceAnnotation(models.Model):
document = models.ForeignKey(Document, related_name='LabellizedDocuments',null=True, blank=True, on_delete=models.CASCADE)
label = models.ForeignKey(Label,null=True, blank=True, on_delete=models.CASCADE)
text = models.CharField(max_length=10000)
text_color = models.CharField(max_length=7, default='#ffffff')
start_offset = models.IntegerField()
end_offset = models.IntegerField()
class Meta:
constraints = [
models.UniqueConstraint(fields=['start_offset','end_offset', 'label'], name='existing-annotation')
]
def clean(self):
if self.start_offset >= self.end_offset:
raise ValidationError('start_offset is after end_offset')
def __str__(self):
return str(self.text)
```
hey, is there a way in flask to hide the url parameters that are shown when i try to login
no
@sturdy pike I'm quite experienced with Flask and web tech. I was looking for a new backend framework to learn and have been exploring asyncio lately.
which webdriver should I download to run selenium on Edge browser?
having an issue displaying sql query content into a table
it pulls the query correctly but not displaying it in my html
<table>
<thead>
<tr>
<th>ID</th>
<th>Load Date</th>
<th>Content Type</th>
<th>Content</th>
<tr/>
</thead>
<tbody>
{% for item in data %}
<tr>
<td>{{item[1]}}</td>
</tr>
{% endfor %}
</tbody>
</table>```
the function:
def staffdashboard():
connection = db_connection_content()
cursor = connection.cursor()
sql_query = "SELECT * FROM roll_call"
cursor.execute(sql_query)
data = cursor.fetchmany(1)
print(data)
connection.close()
return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))
it's displaying the th portion of the code, but not the content of the query
@mortal mango Did you get that error figured out?
when i inspect the html while it's running
@swift sky have you done a print in staffdashboard() to ensure that data has something?
there are no elements from my query present
so i think it's a jinja2 issue
@swift sky have you done a print in staffdashboard() to ensure that data has something?
@devout coral yes it pulls the correct information
@devout coral yeah I did but now I have Internal Server Error
Ok. could you put the following code for me in that stafffunctionboard() function and show me the result. @swift sky
for item in data:
print(type(item))
@mortal mango Do you have debugging set to false?
@swift sky ok now do this for me please:
for item in data:
print(item)
yeah but it's gonna be different data every time
also, I already have a dictionary being passed in my view function
@mortal mango In development you should keep it on yes. But you should not be developing in a public server.
@swift sky you're iterating a dict wrong
@swift sky so do the following:
{% for key, values in data.items() %}
<tr>
<td> {{ value }} </td>
<td {{ key }} </td>
</tr>
{% endfor %}
<tbody>
{% for item in data.items() %}
<tr>
<td>{{item[1]}}</td>
</tr>
{% endfor %}
</tbody>
def staffdashboard():
connection = db_connection_content()
cursor = connection.cursor()
sql_query = "SELECT * FROM roll_call"
cursor.execute(sql_query)
data = cursor.fetchmany(1)
print(data)
connection.close()
return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))
@mortal mango In development you should keep it on yes. But you should not be developing in a public server.
@devout coral I'm on my VPS
I'll transfer the files to my computer and test on my computer and then push it to my VPS
@devout coral you can't unpack a k,v from a dict, you need .items()
@pine yew I know, that is why I edited my thing. I missed the .items()
didn't see your edit - was typing 😛
@mortal mango I mean it does not seem like you care too much about security so just do it on the server.
@pine yew Lol, yeah I also missed a few closing tags for the html
<tbody> {% for item in data.items() %} <tr> <td>{{item[1]}}</td> </tr> {% endfor %} </tbody>def staffdashboard(): connection = db_connection_content() cursor = connection.cursor() sql_query = "SELECT * FROM roll_call" cursor.execute(sql_query) data = cursor.fetchmany(1) print(data) connection.close() return render_template("staffdashboard.xhtml", data=data, dict_to_be_passed=parse_311_sitemap(load_articles()))
@pine yew i did this one
data = cursor.fetchmany(1) do you know what fetchmany returns?
@mortal mango I mean it does not seem like you care too much about security so just do it on the server.
@devout coral yeah I'm not sure how to configure security stuff so yeah
it should be an iterator over db rows
i actually chagned it to fetch all
@mortal mango I mean, it does not matter at this point.
yeah ok
but i havent decided, I'd probably need like the last 3 entries or something
@swift sky what does your schema look like?
so do you know why it says internal server error?
if by schema you mean the dict keys
@mortal mango Just put debbuging to true in your settings.py and you will get a traceback
content_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
load_date = db.Column(db.DateTime)
content_type = db.Column(db.String(20))
content = db.Column(db.String(10000))``` @pine yew
oh you're using an ORM? why are you accessing a DB query directly?
there's a lot of WTFs going on right now lol
thanks
are you using sqlalchemy then?
yeah, what driver you use shouldn't matter
you can just access the model, that's the whole point of the ORM
ahh ok
is your class name Content?
yes
are you using Flask?
yes
Flask-sqlalchemy?
yes
OK! So here goes
content = Content.query.all()
pass that into your render and access the class names as expected
{% for item in content %}
item.content_id
etc
this is covered in their documentation:
https://flask-sqlalchemy.palletsprojects.com/en/2.x/queries/
one second
{% for item in content %}
<tr>
<td>{{item.content_id}}</td>
</tr>
{% endfor %} ```?
hmmm well
is there a way to do this without the orm
because i have some import errors to deal with
and thas a major headache
@pine yew
@swift sky you could use the cursor like you were, but what's your headache with the orm?
I have to refactor my code
ah, I see
i have issues with imports
I guess that's why I was doing it directly
{% for item in data.items() %}
jinja2.exceptions.UndefinedError: 'list object' has no attribute 'items'
no worries
what do you mean?
you fetch it then close the connection
then you render the template with the data you fetched
so I don't know how pymysql does it, but most DBAPI implementations will close your connection when it leaves scope automatically
the fetch calls are generators
they don't do anything until you consume them
the reason i added the close connection call was because my db would overload otherwise
it would keep them open
yeah, flask gives you the bare minimum then tells you to find some extensions
that error being?
{% for item in data.items() %}
jinja2.exceptions.UndefinedError: 'list object' has no attribute 'items'
well you're not dealing with a dict when you use a cursor
you're dealing with rows of a db
which is an iterator of tuples
yeah from what I googled some stack overflow post defined the data in a dict and then explicitly called the key using dotstring
always good to refer to the docs.
https://pymysql.readthedocs.io/en/latest/modules/cursors.html
if you want to access your rows as a dict, you need a DictCursor
i do actually
def db_connection_content():
return pymysql.connect(host=os.environ.get('HOST'),
user=os.environ.get('PYMSQL_USER'),
password=os.environ.get('PASSWORD'),
db='content',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)```
hey
i guess then it depends how im connecting to the db?
wether it's through the ORM or through pymsql
.>
is this possible to make with flexbox ?
@winter spindle no, you need a grid. flexbox is 2D
ohh okay then thanks
er, 1D?
if the box of four is one element
then you could use a flexbox
but that would be very straightforward in grid
oh nvm
i googled some stuff
cursor = connection.cursor(pymysql.cursors.DictCursor)
lets see
@swift sky
def staff_dashboard():
conn = db_connection_content()
cursor = conn.cursor()
cursor.execute('SELECT * FROM roll_call')
data = cursor.fetchall()
try:
fragment = render_template('''
{% for item in data %}
<tr>
<td>{{ item[1] }}</td>
</tr>
{% endfor %}
''', data=data)
return render_template([..])
finally:
cursor.close()
conn.close()
you can also use the name instead
item['content'] or whatever
it's just another thing you inject into your template
yeah
how does it know to place in the []
so instead of
<tbody>
{% for item in data.items() %}
<tr>
<td>{{item[1]}}</td>
</tr>
{% endfor %}
</tbody>
you have
<tbody>{{ fragment }}</tbody>
that's just a placeholder, you put your other arguments there as usual
I was being lazy
yeah
as well as any other args
try:
fragment = render_template('''
{% for item in data %}
<tr>
<td>{{ item['content'] }}</td>
</tr>
{% endfor %}
''', data = data)
return render_template(["staffdashboard.xhtml", dict_to_be_passed=parse_311_sitemap(load_articles())])
finally:
cursor.close()
connection.close()```
but it's not accepting dict_to_be_passed= as a kwarg anymore
or positional, whichever it is
oh ok
return render_template("staffdashboard.xhtml", dict_to_be_passed=parse_311_sitemap(load_articles()), fragment=fragment)
you'll need to change your html
testing
some errors
''', data = data)
line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound:
{% for item in data %}
<tr>
<td>{{ item['content'] }}</td>
</tr>
{% endfor %}
im gonna try with just the [1]
you need to import it from flask
did you pass it into the template?
<table>
<thead>
<tr>
<th>ID</th>
<th>Load Date</th>
<th>Content Type</th>
<th>Content</th>
<tr/>
</thead>
<tbody>
{% for item in data.items() %}
<tr>
<td>{{ fragment }}</td>
</tr>
{% endfor %}
</tbody>
</table>```
oh no
Get rid of the tbody content entirely
just replace it with {{ fragment }} like I suggested above
<table>
<thead>
<tr>
<th>ID</th>
<th>Load Date</th>
<th>Content Type</th>
<th>Content</th>
<tr/>
</thead>
<td>{{ fragment }}</td>
</table>```
well
i managed to navigate to the site
without the server errors
however
ID Load Date Content Type Content
<tr> <td></td> </tr>
that is what was rendered
im gonna get rid of <thead> <tr> <th>ID</th> <th>Load Date</th> <th>Content Type</th> <th>Content</th> <tr/> </thead>
uhh
it just shows up as ```Roll Call
<tr> <td></td> </tr>
so these are not being passed i think
<td>{{ fragment }}</td>
alrighty
getting closer
<tr> <td>testing some content</td> </tr>
thank you @pine yew
@devout coral I turned on debug and it's still saying internal server error
@mortal mango Then it might be something with your server set up. When does the error occur or how does it happen?
ok
let me try running the server again
Guys. I'm fine to run my Flask app only with Werkzeug if I'll get only requests from the same computer at most 20 times a day ? I have read that it's able only to hold 1 request at once, my flask doesn't have any static files because it's an API ?
Guys anyone can help me with django
@peak meadow not a good idea if you don't want the app from randomly crashing or giving bad outputs
Try small webserver like gunicorn or something, they are very easy to setup
Hmm, it's a simple API, the input matters, the output is just a string like "succes"
@abstract kite
@peak meadow ~~still i would recommend a webserver, try this, hardly 2 commands are required to set it up 😅
https://gunicorn.org/~~
@devout coral it still says internal server error
Sure. I will try gunicorn then, thank you @abstract kite
@mortal mango So it is something with your server then. When does the error occur or how does it happen?
@devout coral same error as last time
@abstract kite what are you even talking about
@peak meadow the werkzeug web server is fine for development purposes locally
@abstract kite what are you even talking about
@pine yew ?
Eh my bad, I read it as same network and not same computer. I think I should go get some sleep
@devout coral Do you know why this is happening? I've had this error since yesterday night. I'm pretty sure I have my .conf set up properly.
You running on Ubuntu? With Apache 2?
yeah
And what do they say?
Do you not have a requirements.txt file which you installed all your modules from?
no, I manually installed everything
Well you missed a module. Called encodings
oh so pip install encodings
Yes
it says 404 client error
I suggest you go and find the package in pypi and just copy the download command
ok I'll do that
do you know the package name?
there's a bunch of packages with the word encodings in it
@devout coral
I don't even know what your project is... Let alone a specific package you are using.
My guess is that it is a dependency for one of your other packages.
hmm this
Quart
on Hypercorn
does it multythread, can i share a global class?
like can i have a global class mygame that evry connection can access?
My guess is that it is a dependency for one of your other packages.
@devout coral I haven't done anything. Pretty much all of my code is default.
guess time to test
@mortal mango What do you mean "all of my code is default"?
like the manage.py and the settings.py and the other files
oh ok
As I suggested yesterday. You should first develop your application in a test environment and then later deploy it. Because developing in a "production" environment just adds more variables for when things go wrong.
but I would need apache on my computer then, right?
No. Django has a built in test server.
oh ok, I'll test that then
You don't need to set up apache or anything like that. you just do python manage.py runserver
And if you are using an IDE, like pycharm, the chances of it telling you exactly which package is missing is high.
oh ok. I use VScode.
but is there a way I can get my secret key
I put it in a config.json and I tried opening it but theres nothing inside it
it seems to not have saved
Just generate a new one.
how do I do that?
so it's something wrong with my apache config right?
So no more errors? You were just missing your secret key?
Wait, it is working in your development server and not on the server?
I don't have a virtual environment on my computer
I use the same version of python everywhere so I don't use them
I'll start using them
I've never thought I need them so I didn't
but I'll just pip freeze everything on my computer
Ok, from this day forward please use Virtual Environments. Otherwise if you work with different versions of packages you will run into issues
@mortal mango Do not pip freeze everything
ok I will
Unless this is the only project you ever worked on with third party libraries
I still do not suggest it. Then you will get random packages in your project. Just make a new venv just for this project.
Should be yes
Did you just download the venv files from your server?
So then you will have to create the environment. However, I think VS code has a thing to create it for you
Take a look at that
I do not use VS code so I cannot help too much with that.
Once you got it though let me know
ok
I don't think that makes an environment
I think it just tells you how to change interpreters and stuff
if I move the virtualenv.py file into my exo_api folder, and then I do python virtualenv.py will it activate the virtual environment inside that folder?
That is not how you activate the environment no.
oh
I think it is source path_to_activate_script
in the cmd window in my directory?
I would suggest you put the venv folder in the root of your django project along with the mana.py file
Then cd into the root folder of your django folder and do the activation there
Well I would suggest to make a new one. Then set up vs code for that to be the interpreter for that project.
so delete the one I have right now?
I would suggest you put the venv folder in the root of your django project along with the mana.py file
@devout coral do I make a new folder called venv or is the venv folder somewhere?
The one in your computer yes.
The simplest way is just to make your venv with vs code...
Ok so just make a new vitual environment then follow this to set it up in vs code.
https://stackoverflow.com/questions/54106071/how-to-setup-virtual-environment-for-python-in-vs-code
@mortal mango
oh ok
I have my venv selected
so do I have to do all my cmd commands in vscode for the virtual environment to work?
@devout coral
yeah I have a terminal
so now do I just run it and install everything when it pops up an error
just try to run the server now through that terminal
it worked
so now do I push it to my vps?
wait so how did it work?
I didn't install anything
oh wait nvm
No, now do the pip freeze