#web-development
2 messages · Page 76 of 1
oh, well, cool
I have the following question, I am using heroku and I am using multiple Dynos (Workers), how can I limit the usage of the cronjob to only one dyno?
help! working in Flask. Is there a way to convert a value to float inside JINJA? Im using JINJA to iterate through a dict, printing out different sets of values. Inside my {% for loop %} I need an {% IF %} conditional that compares the value of one of my dict values to an int. My error message says i cannot compare string and int.
@rustic pebble I'd say crontab vs celery should depend on how long it takes to run 1 (or all) members as well as things like how failure states should work
in most cases, I'd recommend celery or something like it to keep track of asynchronous work and make sure it's independent (so 1 failing customer doesn't cause a failure for all 1299 other customers)
@warm igloo right on! thank you!
Welcome. I am an average programmer even after 20 years. But I'm a helluva Googler!
😄
Googler == programmer. Change my mind
@cold anchor Thanks for your answer. Do you think using the session key id (same as in the session cookie django returns) would be the right argument to feed through? Through the API view, I can then see if its a valid session key, then what user it is associated with, and then do whatever from there. It makes perfect sense to me, and the only way it can be exploited is if the attacker has the active session keys of other users (which doesn't seem likely). Do you see any holes in this approach?
seems pretty airtight, though I'll admit I'm not incredibly familiar with django's session safety and how you're passing it around, I'd imagine it's secure
I'll look further into this, thank you for your time. 👍
happy to help!
If I want to add a like feature to my blog with SQLAlchemy
So that I can access user.liked_posts and post.liked_users
Do I need a Like class?
Is there a way to synchronize column width between multiple tables?
@wind walrus thats one way to do it, and has a foreign key for the post, and a foreign key for the user. for django there is an argument for the foreignkey called related_name=, I think for SQLAlchemy its called backref=. hope that helps
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
This is a basic user creation form
I want to add one more field to this, language - and that should be a dropdown of languages
How do I go about doing it?
I think instead of using UserCreationForm, if I use forms.Form, maybe that's doable but I'd like to use UserCreationForm
can't you just add another field under email = at the class level?
yes, but I do not know how to make it a dropdown thing, and also mark it as a non-mandatory field
thanks gary, how do I mark it as non-required field though?
or in other words
make it take a default value in case the user doesn't select any
would I have to replace the Empty value: '' (an empty string) thing in there to whatever I want the default value to be?
it's been a while since i've used django but i believe all field types take an initial value
Thank you for pointing those out! I think I have what I need now ! Thanks
no problem 🙂
@socket.on('connect')
def handle_connect():
print("hello")
why hello is not printing
from the client side it does
socket.on("connect", () =>{
console.log("yea")
})
This is the website trailer which i made for science Exibhition
Do watch it and smash that like button on fb, it would mean a lot to me 😊
I dont know y i m blocked from the projects page but ya this is my project
Please do like it on fb
is there anyone who use twilio
probably
did you upgrade it
Hello, I got an issue in flask, i have written some code and it should display the email when i click the submit button, for some reason it doesnt, ill send you the faulty code and the error
@app.route("/login", methods = ["POST", "GET"])
def login():
if request.method == 'POST':
session.permanent = True
user = request.form['n']
session["user"] = user
flash(f"logged succesesfully", "info")
return redirect(url_for("user"))
else:
if "user" in session:
return redirect(url_for("user"))
return render_template("base.html")
@app.route('/user', methods = ["POST", "GET"])
def user():
email = None
if "user" in session:
user = session["user"]
if request.method == "POST":
email = request.form["email"]
session["Email"] = email
else:
if "email" in session:
email = session["email"]
return render_template("userDisplay.html", email=email)
else:
return f"<h1>Idiot</h2>"
id recommend youd watch the code in discord in full screen to get a better understanding
the error:
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'n'
Id appreciate if you would help me, thanks
ping me if you need anything else or if you want to explain the error to me, so i will pay attention
I hosted my site on heroku but when I visit it I get this Privacy Error
use .herokuapp.com instead of .heroku.com
there is no ssl
@cold anchor thx
your form is malformed @uncut rover
wdym?
Try using loginmanager its much easier
im doing a tech with tim tutorial and im following his instructions
what's in base.html ?
it is the place where you go to log in
how do i make it that when i enter an email, it will show it on a different page, without changing the username
user input**
I'm trying to help you with that
paste the contents of base.html
also read the error thoroughly: KeyError: 'n'
where in your code are you trying to access a dict-like object with the key 'n' ?
No, Im trying to acces email
(name of the dic object)
heres base.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="POST">
<input type="text" name="n">
<input type="submit" value="Submit">
</form>
<form action="#" method="POST">
<input type="email" name="email" placeholder="Email here:" value="{{email if email}}">
<input type="Submit" value="Submit">
</form>
</body>
</html>
My views.py file at this moment that loads an index.html that mentions {{ accname }} and {{ list_name }}, not a single template more
from django.http import HttpRequest
from django.template import Context, Template
from django.template.loader import get_template
def index(request: HttpRequest):
print("yolo")
# return HttpResponse("Testing. This works, apparently.")
temp: Template = get_template("index.html")
context: Context = Context(
{
"list_name": "yeet",
"accname" : "shivacooo"
}
)
return temp.render(context=context)```
The error I'm getting is `dictionary update sequence element #1 has length 3; 2 is required`
Additional information on screenshot if needed
why do you have 2 separate forms @uncut rover ?
I dont
Idk
Im using tech with tim to learn flask, and he did 2 forms, so i did the same
you might be submitting the form with an email, but you're accessing the 'n' key from the form, which looks like it doesn't exist if you don't fill out that part of the form
hence, the 400 response you see
SO you say i need to put the email in the same form?
no, that's not what I'm saying
do you know why you're getting the error that you're getting?
Because it thinks im activating the 'n' keyword and not email
thats the error, right?
correct, you are accessing 'n' and not 'email'
in the third line of you login() view
ok, im going to my grandparents rn, ill be in 2hrs back
well continue in 2hrs, im going to gp's house
I'll be at work, won't be available
What should I use to make micro service?
I can handle celery rabbit docker and scrapy for backend and even attach sqlalchemy with postgresqle,
but how should I write app that receives requests and parses images from some given urls?
Can I do something like this in django, some textbox that receives lists of urls and sends command or there is easier way?
Hi, I am trying to redirect the user to the his question's web page whenever he asks a question but it is giving me an issue
@login_required(login_url='/user/register/')
def ask_question(request):
FormSet = inlineformset_factory(User, Question, fields=('title', 'text', 'tags'), extra=1)
form_set = FormSet(queryset=Question.objects.none(), instance=request.user)
if request.method=="POST":
form_set = FormSet(request.POST, instance=request.user)
if form_set.is_valid():
form_set.save()
id = form_set.cleaned_data['id']
return redirect(f'/question/{id}/')
return render(request, 'questions/askquestion.html', {'form_set':form_set, })
the error I am getting when submitting the form is : bash list indices must be integers or slices, not str
@flint breach
im at work rn
nvm it works
@late fjord i believe form_set is a list of forms, so querying it for the id doesn't make sense, you have to loop over the list first and then get the id
im not entirely sure tho
@cold anchor it works now, just doesnt display the email, when i log in with email, it recognizes that im in, and displays a message
Is anyone available to help me with a quick issue?
UserNav: https://pastebin.com/fzVhnztK
Header: https://pastebin.com/Ad3sjgPe
App: https://pastebin.com/SE2PJ9RP
Why header UserNav part is not getting updated after log in/out?
idk i suck at web development are you using django?
Yes, Django as backend, because this is Code Jam project
Ahh
yea I dont know django
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, welcome to the Lumber Legend's main page!"
@app.route("</name>")
def user(name):
return f"Hello {name}"
if True:
app.run()
Traceback (most recent call last):
File "website.py", line 10, in <module>
def user(name):
File "D:\codyk\coding\.Aprojects\PythonMain\venv\lib\site-packages\flask\app.py", line 1315, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "D:\codyk\coding\.Aprojects\PythonMain\venv\lib\site-packages\flask\app.py", line 98, in wrapper_func
return f(self, *args, **kwargs)
File "D:\codyk\coding\.Aprojects\PythonMain\venv\lib\site-packages\flask\app.py", line 1275, in add_url_rule
rule = self.url_rule_class(rule, methods=methods, **options)
File "D:\codyk\coding\.Aprojects\PythonMain\venv\lib\site-packages\werkzeug\routing.py", line 666, in __init__
raise ValueError("urls must start with a leading slash")
ValueError: urls must start with a leading slash
I imported redirect and url something. I think that was the problem but I dont know how to fix it now.
Please mention me ^^
@elfin peak heres a better code, if you want your stuff to be on the web, and generally a better way of coding in flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return f"<h1>Hello, welcome to the Lumber Legend's main page!</h1>"
@app.route("/<name>")
def user(name):
return f"<h1>Hello {name}</h1>"
if __name__ == '__main__':
app.run(debug=True)
@elfin peak ```py
@app.route("</name>")
should probably be "/<name>"
Oh, im just recommending a better way, to make your code more readable
np
I just needed it to work so I can keep learning lol
tech with tim?
yesir
me too
Is there any way to actually view the rows you have in an SQLAlchemy table?
Because when I try to query my table I don't get any useful info.
yeah
You have to deserialize the data
I prefer using Marshmallow for it
Would it be possible to do online, via a DB browser of some sorts?
What do you mean online?
And thanks for the suggestion, just looked at Marshmallow looks good. I'll try it
I usually use Marshmallow + SQLAlchemy for anything DB related
Much appreciated, thank you @native tide That's solved it for me
Guys I am using Flask to intgrate mongodb to html ages. I need help please.p
This is my mail html page. If I click on the more button, I am redirected to
to this page
my backend code is here.
It throws the above error. However, nothing seems to be wrong here.
Any suggestions please
what's the full error and stack trace? that's just a part of the stacktrace, the error should be at the very bottom (and at the top I believe)
Hey @zealous cloud!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
• If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
• If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
Hello everyone can someone help me with this flask and json issue I've encountered? I have this json object returning me the names of govt. officials. I would like to iterate over the json object that contains each senators info and extract the senator id, first and last name. Im currently making an <li> element for each, but i will organize this into a table later. The issue is that I'm having difficulty extracting all the info I need. Currently I'm only getting the id, first name, and last name of the first member and its printing this item three times? Can someone please look at my code in this file? Line 1-15 are the flask route, lines 20-28 are the html jinja template, and the last block is how the json object is organized.
https://paste.pythondiscord.com/ecikiqerip.cs
I think you need to loop through the json object
Im struggling with this at the moment. Not much experience with json objects. my json object is in my flask route as data = res.json(). Should i loop over this inside the flask route? Or should I use a list comprehension to extract every id, first name, and last name?
I think you need to load in your json as a python dic using the below,
data = res.json() loaded = json.loads(data) members = loaded['members']
Then your return would be this, as you can make available all the members details.
return render_template('search/gov-officials.html', members=members)
Then in your template file, you'd put something like to go display each object field you want from members
{% for member in members %} {{ first_name }} - {{ last_name }} {% endfor %}
Can someone help me this error please
I'm not 100% certain, but if you use ctrl+shift+p and type in "convert" you should get either convert indent to spaces or tabs. That may fix it.
There seems to be a mix (of both tabs and spaces) in your file, which is the issue.
I'm not 100% certain, but if you use ctrl+shift+p and type in "convert" you should get either convert indent to spaces or tabs. That may fix it.
@native tide Thank you soooooooo much. It really worked. Thanks a lot.
I'm glad! No worries.
@elfin ridge im trying this now and I'm getting error - the JSON object must be str, bytes or bytearray, not dict. The error is coming from the loaded = json.loads(data) line. I did a for loop in my route and figured out how to get the data. Now i just need to figure out how to store all the ids, first names, and last names so I can create an <li> for each in my jinja template. I'm gonna try a list comprehension first
Thanks for helping
@zealous cloud not sure my code is right and I admit I'm no expert. But I think essentially what you need to do is 1) convert your json to a python list, then 2) pass that python list to your template, then, assuming you're using Jinja2, 3) iterate over that python list in your template.
So i might be wrong, but that's what I essentially did. I made a list comprehension and stored it in a variable for all three of the items i needed. Then I passed them to my return render_template and I called them in the jinja template and it worked. It's a bit ugly and the moment but it worked. I'll refactor it late. Thank you for the help
Ok, glad it works! No probs
Hey all, I'm looking for a way to have an error box/message pop up when a user enters the wrong password while logging in, however I don't know how I'd pass the error from Tornado onto the frontend ***without *** having to reload or change the page -- I was reading about using websockets but unsure if I could pass the error this way, and had another suggestion to utilize js, which while sounds great, i'm not certain would work. What would you suggest? (py3.8, tornado 6)
you ready to use javascript? because you'll need to use javascript
absolutely!
that's actually what I was hoping for, just lost on how to connect the two without a reload/change.
so the most standard way is to send a request to your API from javascript and show the error element if/when it fails
would you happen to be able to link an example for that process, or a resource I could look to?
Here’s an example signup form I made in react if that’s helpful https://github.com/Rdbaker/metrecord-ui/blob/master/src/views/login/emailSignup.js
That's wonderful, thank you very much!
@384158530623635456 Like this? https://github.com/seejohnrun/haste-server
Yeah your nickname makes pinging impossible. Sorry
@dapper raft
@dapper raft Well it's not written in Python. You can use that code as a framework to build your own.
Hmmmmm
Does anyone know how to redirect to a internal 404 page?
def page_not_found(error):
return render_template('page_not_found.html'), 404```
I have tried this but it doesnt work
In Twitter, is it possible to see traces of APIs in the source page?
How to validate Email format in DRF serializer?
@valid cypress you can set the serializer field as EmailField e.g. email = serializers.EmailField()
Django, API, REST, Serializer fields
How do I prevent people from removing required tags / deleting javascript from google captcha from django side?
I want to make it so it won't send the message until the check is pressed
Now they can remove just 3 parts of the code and vola they can spam all they want
Not sure what your code looks like exactly, but you'd want to something in your views.py which requires the success response back from google captcha. Only if it gets the success response back does then the message get sent.
@alpine tinsel How does it 'not work'?
How do I prevent people from removing required tags / deleting javascript from google captcha from django side?
@elder nebula serverside validation, you can't really control the removal of tags - as it is client side
hey, im doing sql alchemy now, and it says for some reason in my class, that db doesnt have certain properties, and more
check it out
db = SQLAlchemy(app)
class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column("name", db.String(100))
email = db.Column("email", db.String(100))
def __init__ (self, name, email):
self.name = name
self.email = email
@uncut rover can you share the exact exception your receiving?
it says that those properties for db dont exist
Column, string and integer
@elfin ridge ^
Have you imported SQLAlchemy?
from sqlalchemy import Column, Integer, String
if you're using Flask, then it would be from flask_sqlalchemy import SQLAlchemy
Yeah, did that
from flask_sqlalchemy import SQLAlchemy
can i do maybe from SQLAlchepy import Column, String integer?
@elfin ridge
^
maybe i need to install extension SQlite?
im using VScode and i dont have that extensiom
extension
if app was wrong, my whole code would not work
no, doesnt work
have you provided a sqlite database uri? e.g. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
class users(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
isnt it meant to be like that
@uncut rover
Can you paste that class in and see if it works
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///users.sqlite3"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
the database i provided
db = SQLAlchemy(app)
class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column("name", db.String(100))
email = db.Column("email", db.String(100))
def __init__ (self, name, email):
self.name = name
self.email = email
the class
Try change it to this
class users(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
email = db.Column(db.String(100))
Without the quotations at the start essentially
i think the issue is with db, that what it says
it says db has no instances of Column, string and integer
thats weird, it should have that
did you do pip install flask_sqlalchemy in the terminal
no
I don't know, but is it right that you put the names of the columns in your user model? I've never seen that before
i did do that, and it didnt work
I don't know, but is it right that you put the names of the columns in your user model? I've never seen that before
@native tide it's valid to do that, shouldn't cause issue
@uncut rover could you copy exactly the exception your seeing when running your app? will help us diagnose you issue easier
Did you create the table after setting it up?
In the python terminal from [filename] import db
then
db.create_all()
Line 77, I'm not fully sure but I thought db.create_all() can only happen from the shell
wdym?
the error was before i did db.create_all()
The class looks right to me, have you tried to remove
self.name = name
self.email = email
and see if it works?
I am trying to send my token in json format in request.data in my flask API. But I get this error TypeError: a bytes-like object is required, not 'str' // Werkzeug Debugger anyone knows how to solve it?
idk why it's not working
so does anyone have a solution?
guys i'm totally new, what's the main difference between Django and Flask?
Django is a framework and Flask is a library
Flask handles one thing: exposing select Python functions over the internet
Django does that, along with object-relational mapping (allowing you to manipulate your database with Python objects), providing an admin frontend, authentication, random utilities that let you do stuff like send email, etc.
so Flask is better if your project is really small or you're experienced enough to know what parts you want to provide all the things that it doesn't
Thanks a lot, i was very confused. 😄
I'll choose Django, my project has a big gui and i wanna do it web
also i have database so
really thank you 😃
if you have a separate frontend
you might wanna look into DRF
django-rest-framework
Heyo, could anyone tell me if I correctly understand what happens during a database migration? Here's my understanding:
During a database migration, "migration files" (similar to commits) are made for each of the changes made to the ORM (or is one file made on each python3 manage.py makemigrations command?). Then, after you can apply it to the actual database schema using those files.
Hey, I think I'm being a total idiot. I am developing a flask app which should populate a chart.js chart. I have the sample chart loading nicely using the following code from the template, but how do I actually populate my own data in there? I can't see anywhere in the page of the CSS that is actually adding the data.
<div class="chart">
<!-- Chart wrapper -->
<canvas id="chart-bars" class="chart-canvas"></canvas>
</div>
@app.route("/create_label", methods=["GET", "POST"])
def create_label():
if request.method == "POST":
data = request.get_json()```
This is the beggining of my route, however when it is called I get `UnboundLocalError: local variable 'request' referenced before assignment`. How is that possible?
from flask import Flask, request, redirect, session, url_for, Response
Webinar: “Django Database Performance Tips” with Andrew Brookins
Django is one of the most popular Python Web Frameworks. However, optimizing django performance can often be difficult. Most performance problems boil down to one thing: the database. According Andrew Brookins, the only way to master Django performance is to master database performance.
In this Webinar, Andrew is going to take us on a journey through an app that he created to demonstrate all the bottlenecks that arise when trying to optimize your Django app.
Andrew Brookins has over a decade of experience with Django and Relational Databases. That experience spans DevOps, application development, and platform engineering. He has worked on ecommerce sites, mobile APIs, comic book readers, particle simulation systems, learning and volunteering apps, and now works on a Databases as a Service at Redis Labs. Andrew has also authored "Temple of Django Database Performance" which dives into the even greater detail than this webinar does. Furthermore, he has recently released a course on "Redis for Python Developers" at Redis University.
The webinar is on Monday 10th August at 17:00 UTC, find this in your timezone at http://time.unitarium.com/utc/1700
Register at https://info.jetbrains.com/pycharm-webinar-august-2020.html
Heyo, could anyone tell me if I correctly understand what happens during a database migration? Here's my understanding:
During a database migration, "migration files" (similar to commits) are made for each of the changes made to the ORM (or is one file made on each
python3 manage.py makemigrationscommand?). Then, after you can apply it to the actual database schema using those files.
@untold flare preety much correct, a migration file simply maps to each app, (one migration can have multiple changes inside), and when you apply it, you make the actual change
I'm guessing you wouldn't call the summary but something else...Check their documentation
this is what it looks like it and has loads more
can I make in Flask simple gui for text input and few buttons ?
this is what it looks like it and has loads more
@crystal stirrup have you tried scrolling along to the right?
🤦♂️ im new to coding and didn't notice that small scroll
haha - no worries - easy mistake, it doesn't wrap the text
Would it be a bad idea to store a user's ID (primary key of database) in a session? Would they be able to see it?
On submitting a form with my flask app running on heroku, I'm getting 400 Bad Request: CSRF token missing or 400 Bad Request: CSRF token is invalid but I'm pretty sure I have the token. ( {{ login_form.csrf_token }} -> <input id="csrf_token" etc...>) What should I do?
Are you using FlaskForm @languid shard
And have you set your app.config['SECRET_KEY'] to something
- Yes
- In config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = 'secret-key-hidden'
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
class ProductionConfig(Config):
DEBUG = False
class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True
class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True
I'm not sure if app.py draws from this though
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.from_object(os.environ['APP_SETTINGS'])
app.secret_key = os.urandom(24)
db.init_app(app)
csrf = CsrfProtect(app)
app.config['SESSION_COOKIE_SECURE'] = False
@languid shard you may need to set your server name in your config.py e.g.
SERVER_NAME = "yourapp.heroku.com"
I just realized that I set the secret key up in two places
@elfin ridge I'll try that next
I had deprecated csrfprotection syntax
Would it be a bad idea to store a user's ID (primary key of database) in a session? Would they be able to see it?
@native tide If it's stored in a session cookie, yes, they'd be able to see it. I guess it doesn't matter as they can't use the user ID by its self. It's depends what you're using it for. A user can edit a cookie easily, so if you're reading their user ID from the session and they edit it to someone else's user ID, you got a bit of a security issue there.
@native tide it's unusual for user id's to be secret, anyway
I had deprecated csrfprotection syntax
@languid shard glad you found the issue!
Unfortunately, as it often is, I found another issue 😦
the more important question is whether the session is stored directly in the cookie or not
I don't think it actually is, so @elfin ridge 's warning may be premature
400 Bad Request: The CSRF tokens do not match.
It's on a specific part of the code/html though
{% for entry in leadership %}
<tr>
<td>{{ entry["year"] }}</td>
<td>{{ entry["activity"] }}</td>
<td>{{ entry["role"] }}</td>
<td>{{ entry["level"] }}</td>
<td>{{ entry["duties"] }}</td>
<form action="/{{ user }}/" method="post">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input name="activity" value="leadership" hidden>
<input name="delete" value="{{ entry['id'] }}" hidden>
<td><button class="btn btn-danger" type="submit">Delete</button></td>
</form>
</tr>
{% endfor %}
When I click the Delete button, it greets me with csrf_token mismatch
Now I get 400 Bad Request: The CSRF session token is missing.:
<form id="leadership_form" action="/{{ user }}/activity" method="post">
<div class="row">
{{ leadership_form.csrf_token }}
<div class="col">
{{ leadership_form.year(class_="form-control", placeholder="Year") }}
</div>
<div class="col">
{{ leadership_form.activity(class_="form-control", placeholder="Leadership Activity") }}
</div>
<div class="col">
{{ leadership_form.role(class_="selectpicker") }}
{{ leadership_form.level(class_="selectpicker") }}
</div>
<div class="col">
{{ leadership_form.importance(class_="form-control",placeholder="Responsiblities") }}
</div>
</div>
{{ leadership_form.lead_submit(class_="btn btn-primary") }}
</form>
# Configure application app = Flask(__name__) # Ensure templates are auto-reloaded app.config["TEMPLATES_AUTO_RELOAD"] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config.from_object(os.environ['APP_SETTINGS']) app.secret_key = os.urandom(24) db.init_app(app) csrf = CsrfProtect(app) app.config['SESSION_COOKIE_SECURE'] = False
@languid shard just noticed you're using os.urandom(25) to generate your secret key, see here why this isn't a good idea: https://stackoverflow.com/questions/27287391/why-not-generate-the-secret-key-every-time-flask-starts
Ah ok!
I used to do that, so maybe...?
You mentioned this is an issue when running on Heroku, so I assume you don't have this issue when running locally?
correct, however, now that I linked the db to Heroku I don't know how to run locally anymore
But last time I ran it, I didn't come across this
@languid shard you may need to set your server name in your config.py e.g.
SERVER_NAME = "yourapp.heroku.com"
I'm assuming this didn't solve your problem then?
Also, are you hard coding your db config? If you store the DB config in environment variables, this would mean you could run the same code locally and in Heroku without issue.
I think the env variable is on the dyno, not on my local machine
I just started this yesterday, so I don't really know what I'm doing
Considering you started yesterday and you're already connected it to a db and running on Heroku, I'd say you're doing pretty well
I had the web app completed a few weeks ago, but I haven't hosted anything really before
Ok, it's really weird - it basically randomly works and doesn't work
What error are you getting now? I've lost track
These two: 400 Bad Request: The CSRF session token is missing. & 400 Bad Request: The CSRF tokens do not match.
However, sometimes it works and doesn't give these errors
This is my web app: https://safe-bayou-60879.herokuapp.com/
Registering as a manager doesn't work for me
I registered as manager fine, logging in however I was met with a 400 error
that's weird
is there anything in the logs which reveal the issue a bit more?
there's only one dyno running for the service as well I assume?
Yes
Managed to login in as a manager fine now
...?? I don't know what's happening
I changed the key after I pushed
I thought I put it into .gitignore though 🤷
I think it's an issue with the Heroku setup
I've seen issues like this before where you're running large apps on multiple servers and the user gets routed through to a instance of the app which they weren't previously being routed through to, so it doesn't recognise the session
Heroku may be doing something similar, even though you only have one dyno running
Just trying to refresh my heroku knowledge now
hmmm
You may need to set the SESSION_COOKIE_DOMAIN = "safe-bayou-60879.herokuapp.com" as well
Pushing to heroku rn
I think it didn't work. One thing I'm noticing is that the apology isn't secure
yo i had reset my django database by deleting migration files and the sqllite file did i do it correctly?
However, whenever it works it's always https secured
It's only say it's not secure due to the image link is over http rather than https
yo how to reset the database in django (sqlite)
@elfin ridge oh yes yes thanks sir
@elfin ridge btw is it the standard way of doing it?
I think so, if you're looking to start afresh it's probably the cleanest way
@elfin ridge thank you again sir
No probs
@elfin ridge any advice for me moving forward?
Has anyone deployed Quart to heroku before? I am getting the following error: ModuleNotFoundError: No module named 'quart.worker'
good evening everybody!
have any of you guys ever tried deploying your django app + postgresql on remote host using docker? im really stuck and seeking help 😦
I'm hosting a web app on Heroku, but I'm often getting these two errors when posting forms: 400 Bad Request: The CSRF session token is missing. & 400 Bad Request: The CSRF tokens do not match.
What's strange is that sometimes it works and sometimes it doesn't. https://github.com/codeNinjaDev/recordbook-auto
Web app; https://safe-bayou-60879.herokuapp.com/
I asked this earlier and this was the response after some digging:
I've seen issues like this before where you're running large apps on multiple servers and the user gets routed through to a instance of the app which they weren't previously being routed through to, so it doesn't recognise the session
Heroku may be doing something similar, even though you only have one dyno running
If i have 10 lines of code in a single div with class "a" or 10 divs with 1 line each with class "a" and i want to use css to style "a"
Does it make a difference in terms of performance, speed, or memory usage?
Or is it the same
If i have 10 lines of code in a single div with class "a" or 10 divs with 1 line each with class "a" and i want to use css to style "a"
Same content, same class, in 1 div or in 10
good evening everybody!
have any of you guys ever tried deploying your django app + postgresql on remote host using docker? im really stuck and seeking help 😦
@polar coral I might be able to help, if you let me know what exactly your issue is
@elfin ridge About my heroku web app earlier, I figured out how to run it locally (heroku local) and it works perfectly
But running it on heroku sucks
dyno
nvmd found it
@elfin ridge About my heroku web app earlier, I figured out how to run it locally (
heroku local) and it works perfectly
@languid shardI must admit I'm a bit stumped, I've been researching it a bit to not much avail.
Same 🤷 It's frustrating
A shot in the dark a bit, but you could try enabling sticky sessions with heroku features:enable http-session-affinity
That's usually a potential solution for this issue when you've got lots of dynos running, if it doesn't work then you can just disable it again with:
heroku features:disable http-session-affinity
yeah that caused an internal 500 error
I am trying to build a django website that takes in users job resume info in input forms and displays it back in an easy to copy and paste format. I have 4 resume sections, say skills, work history, references, and contact info. I am trying to allow the user to dynmically add the sections to their resume in the order they choose.
Each section has a button at the top of the page that allows adding. How do I dynamically add the sections without reloading the page? Do i have to store the sections in my JS file? I am hoping I can pull them from my backend
@elfin ridge nvm that's something else
I am trying to build a django website that takes in users job resume info in input forms and displays it back in an easy to copy and paste format. I have 4 resume sections, say skills, work history, references, and contact info. I am trying to allow the user to dynmically add the sections to their resume in the order they choose.
Each section has a button at the top of the page that allows adding. How do I dynamically add the sections without reloading the page? Do i have to store the sections in my JS file? I am hoping I can pull them from my backend
@vast remnant You'll need to do some form of JS to do that. Simplest way may be to use jQuery.
A bit dated tutorial, but this may help: https://realpython.com/django-and-ajax-form-submissions
@elfin ridge nvm that's something else
@languid shard guessing it didn't work?
In django, what does it mean if something is in INSTALLED_APPS. I know what an app is, but what goes there, and what does that do.
I know some about it, but, it would be nice to know more because usually I just hear that it is something "you have to do"
INSTALLED_APPS provides a list of places where Django should look for models, management, commands, tests and other utilities.
So say your Django project consisted of two app like 'polls' and 'bookings'. If you didn't include 'bookings' in INSTALLED_APPS then Django wouldn't trigger migrations relating to models in 'bookings', would be able to access their tables as they wouldn't be there, if 'booking' app had static files, when running collectstatic Django wouldn't find the static files associated with that app.
Same with tests, management commands etc in that app. Where as for 'polls' it would find all these things as it was listed under 'INSTALLED_APPS'.
Likewise for Django packages you may want to include like DRF, unless they're included in INSTALLED_APPS, Django won't register them.
--HELP--
I am making a web site to run on my rpi. I tried to add a feature to see what is my rpi's cpu temperature but my html, instead of the actual value, says None.
home() has, inside of it a variable that measures with rpi's GPIOZERO CPUTemperature function.
It worked on it's own, but when I added it to this code it stopped working.
This is the html code.
This is the output of the html.
2nd line, says, Processor temperature: None
If anyone know how to fix this pls @ me 😬
Hello there! I'm running a web application here, but I don't seem to catch the error in here 😦
Here's a snippet of my flask code, also accompanying with it a library called CS50 which fetches a query from my database code in the method called execute.
Anddd, jinja tells me that the placeholder variable user is undefined, even though the variable database has a content with a dictionary containing my SQL queries 😦
I don't know where to look at anymore, helppp
@wintry flicker seems like that it cant find the user var and i think its because of your (% for user in database %) but its {% for users in database %}
@elfin ridge Sorry, got stuck in a meeting. It doesn't break it, but it doesn't fix it either
@wintry flicker seems like that it cant find the user var and i think its because of your (% for user in database %) but its {% for users in database %}
@nimble epoch
Changed it to users
didn't help either
@wintry flicker Did you change it to {% ... %}?
What do you mean?
Change your (% for users in database %) to {% for users in database %}
ah
And change it for endfor too
when hosting a discord bot, is it a good idea to allow IPv6? im not all that sure where to put this question lol
Hey quick question about "best practice" when making a web application with Python. I started making an website with cPanel as a personal project to learn about web development so I was just putting all my HTML, CSS and JS inside public_html. If I am now starting to make a flask app should I put that too inside the public html? Or should I put that in a different folder and point it to public_html or should I just forget public_html (delete it) and put it inside the "public" folder Flask creates when you make an App
I know this donest matter but I just wanted to know what most people do
Imagine someone looks at my file structure and they see my index.html is on the var folder now
:^)
HELP FOR PROJECT DEPLOYMENT ON HEROKU
I had created a Django project which I want to deploy on Heroku. I am following the steps provided on https://medium.com/@jntushar/how-to-deploy-a-django-application-on-heroku-19b34bda1991 blog.
I have completed all the steps until step-11. Although, before setting up this environment, my project was working properly but now it shows me an error like import markdown
ModuleNotFoundError: No module named 'markdown'
I searched on the internet and as a solution, I made the requirements.txt in which I provided the markdown version and other requirements using the command '''pip freeze > requirements.txt''' but no progress.
Can anyone help me to deploy my project or should I use any other easy method?
My project is working properly on my pc!
Make sure your Django application is ready to deploy and running well on localhost, run python manage.py runserver on terminal.
If I want to create a mailing list for my subscribers
I should send each email separately right? I can't just add everyone as recipient or even as bcc
So should I thread the emails then?
I can't get autoplay to work with html audio tag, it gets blocked by browser. If you know any hack around it ping me
Does anyone know how to take your dash app with all your selections and export a shareable url, that someone else can click and get the exact same view? (From the end users perspective)
Hi guys any library recommendations for validating the input in HTTP request bodies?
Currently looking at Flask-inputs but am wondering if there's a better alternative
I'm looking to add a very 'Show More' basic functionality, however I'm not quite sure how to do that.
Here is my simplified template code
{% extends "MyApp/base.html" %}
{% block content %}
{% for post in posts %}
<p>{{ post.summary }}</p>
{% endfor %}
{% endblock content %}
Now what this will do is list out all the posts from my posts database
What I'd like to do is for it to list out the first 10, and then there be a show more, which lists out another 10(or less than 10 depending on how many are left)
What do I add to this code to achieve that?
posts is not a database, it's a table in your database
had to iron out that quirk hehe
so basically, you want pagination?
oh, I'm sorry about that
basically yea, you list the first 10
Right, what then?
then if you want more, you click and go to the next page
and it shows you the next ten items
if you want it to be dynamic, you'll need ajax (aka no site refreshing)
if it can be static, pagination is what you need
django docs 🤷♂️
the ajax thing as well?
the pagination 100%, the ajax probably aswell, if not on django docs then on stack overflow or whatever
google will be your friend 🙂
oh okay, thank you : )
oh wicked, it's just a simple paginate_by attribute in a class based view that'll do the trick
Brilliant! I'd have never gotten the name for that without you having told me !
if you're doing anything that seems very common, django probably has a shortcut for it
pagination, generic views, etc etc
If I'm using S3
Do I replace all the Jinja2 {{url_for('static', filename='xxx')}} with s3 links?
how do i make JS document.getElementById.innerHTML = "foo" and document.getElementById.style.color = "blue" "constant"
so that theyre on the server
and after reloading the page the results stay the same
im unsure what you're trying to do
also getElementById is a function, you're calling it wrong afaik?
so say i have
<p id = "value">CHANGE ME</p>
<button type = "button" onclick = "document.getElementById('value').innerHTML = 'FOO';" >CHANGE TO FOO</button>
<button type = "button" onclick = "document.getElementById('value').innerHTML = 'BAR';" >CHANGE TO BAR</button>
when i press the foo button the text will be foo
but when i reload the page itll be CHANGE ME again
well, a few options are storing it in a cookie or HTMLLocalStorage, perhaps retaining it to a database, or maybe some state handling like react/redux or vue
but this all feels overkill
this is just for an experiment, or any particular reason why oyu wanna handle it with js
anyone can explain me what a local webserver is?
Is it possible to have both a static folder for development localhost purposes and also use Amazon S3 link?
@wanton ridge local webserver = webserver running on your computer
local environment = your own computer
web server is just a computer connected to a network that communicates with other computers in the network via network requests
how do i load json from javascript?
let requestURL = '...data.json';
let request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
const jsonData = request.response;
loadingData(jsonData)
}
function loadingData(jsonObj) {
document.getElementById('monday').innerHTML = jsonObj['monday'];
document.getElementById('tuesday').innerHTML = jsonObj['tuesday'];
document.getElementById('wednesday').innerHTML = jsonObj['wednesday'];
document.getElementById('thursday').innerHTML = jsonObj['thursday'];
document.getElementById('friday').innerHTML = jsonObj['friday'];
document.getElementById('saturday').innerHTML = jsonObj['saturday'];
document.getElementById('sunday').innerHTML = jsonObj['sunday'];
document.getElementById('monday').style.backgroundColor = jsonObj['c_monday'];
document.getElementById('tuesday').style.backgroundColor = jsonObj['c_tuesday'];
document.getElementById('wednesday').style.backgroundColor = jsonObj['c_wednesday'];
document.getElementById('thursday').style.backgroundColor = jsonObj['c_thursday'];
document.getElementById('friday').style.backgroundColor = jsonObj['c_friday'];
document.getElementById('saturday').style.backgroundColor = jsonObj['c_saturday'];
document.getElementById('sunday').style.backgroundColor = jsonObj['c_sunday'];
}
i have this but it does not work
{
"monday": "-"
"tuesday": "WTORECZEK"
"wednesday": "-"
"thursday": "-"
"friday": "-"
"saturday": "-"
"sunday": "-"
"c_monday": "whitesmoke"
"c_tuesday": "whitesmoke"
"c_wednesday": "black"
"c_thursday": "whitesmoke"
"c_friday": "whitesmoke"
"c_saturday": "whitesmoke"
"c_sunday": "whitesmoke"
}
this is data.json
oh did i forget commas
Uncaught TypeError: Cannot read property 'monday' of null at loadingData (scripts.js:13) at XMLHttpRequest.request.onload (scripts.js:9)
line 13 is document.getElementById('monday').innerHTML = jsonObj['monday']; line 9 is loadingData(jsonData)
Guys I have a question : Does google's search engine have an API?
Like I wanna get the search results .
I know that www.google.com/search?q=the+search does the job but does it have an API?
I want to make a website that shows search results from google , but with my design
Or even from duckduckgo
Guys I have a question : Does google's search engine have an API?
@glass sandal Yes - https://developers.google.com/custom-search/v1/introduction though I believe only the first 100 requests are free
I see
So only 100 requests?
And another question , I couldn't scrape the website itself
Like I use BeautifulSoup
"Custom Search JSON API provides 100 search queries per day for free. If you need more, you may sign up for billing in the API Console. Additional requests cost $5 per 1000 queries, up to 10k queries per day."
And another question , I couldn't scrape the website itself
@glass sandal against TOS
And when I do :
soup.select(".g")
It returns nothing
What is TOS?
Like it doesn't allow me to scrape it?
TOS = terms of service
Oh
i missed a semicolon at line 9...
and you wont get help here either
I mean , I wouldn't share the website to influence from it tho
Like I want it for myself
I see
The rules...
Well it's not illegal, as it's not against the law, but Google don't allow it. If you did it though the most Google would do would probably just block your IP
It's a limit of 100 per a day, so 3,000 requests a month
You can ask here, I'm not sure why crazygmr101 is saying you won't get help here
I see
So
I am using bs4 to extract the data
And it returns an empty array
Like the search item's classes are g
And when I do soup.select(".g")
it just returns an empty array
Even soup.find_all didn't work
I guess it's a css problem?
Or like the search item is so deep in the code?
sup guys, been stuck on this for ages. im trying to return the value of an attribute when web scraping in BeautifulSoup but im getting no where
soup = bs4.BeautifulSoup(res.text, 'lxml')
pid = soup.find_all('meta', attrs={'name':'branch:deeplink:productId'})
print(pid['content'])```
the attribute i want the value of is 'content'
but i cant get anything to work to return the value of it
find_all returns a list of instances not an instance itself, afaik
either pid[0]['content'] or soup.find.. instead of soup.findall
so i got so far, now run into another issue
soup = bs4.BeautifulSoup(res.text, 'lxml')
product = soup.find_all('a', attrs={'data-qa':'product-card-link'})
for p in product:
product_title = p.attrs['aria-label']
print(product_title)
product_link = p.attrs['href']
product_link = ('https://www.nike.com'+product_link)
print(product_link)
#product_titles.append(product_title)
#product_links.append(product_links)
res1 = requests.get(product_link)
soup = bs4.BeautifulSoup(res1.text, 'lxml')
pid = soup.find('meta', attrs={'name':'branch:deeplink:productId'})
#x=0
productid = pid['content']
#x=x+1
print(productid)```
it runs the first 2 products then gets hit with this error:
https://www.nike.com/launch/t/air-jordan-1-satin-red
79f50e18-dacf-560e-bb64-4df906c374fd
Air Jordan 1 Retro High OG CO.JP 'Tokyo' Release Date
https://www.nike.com/launch/t/air-jordan-1-retro-hi-tokyo
3d24bc12-2e71-5db9-ada9-6abf9cf13ede
PG4 x Gatorade 'GX Orange' Release Date
https://www.nike.com/launch/t/pg4-gatorade-gx-orange
Traceback (most recent call last):
File "C:/Users/mattr/OneDrive/Documents/Coding zip/Coding/Python IDLE/bs4 snkrs early link test.py", line 30, in <module>
productid = pid['content']
TypeError: 'NoneType' object is not subscriptable```
pid['content'] is equal to None['content'] in this case, pid didn't find anything
or should i say soup.find(...) didn't find anything
would you know why? because it is there in the html
name is branch:deeplink:productId?
yep
care for a screenshot
of course
1 second
it finds it for the first 2 products, then fails
ive changed the code to find_all but im now getting hit with this:
https://www.nike.com/launch/t/air-jordan-1-satin-red
79f50e18-dacf-560e-bb64-4df906c374fd
Air Jordan 1 Retro High OG CO.JP 'Tokyo' Release Date
https://www.nike.com/launch/t/air-jordan-1-retro-hi-tokyo
3d24bc12-2e71-5db9-ada9-6abf9cf13ede
PG4 x Gatorade 'GX Orange' Release Date
https://www.nike.com/launch/t/pg4-gatorade-gx-orange
Traceback (most recent call last):
File "C:/Users/mattr/OneDrive/Documents/Coding zip/Coding/Python IDLE/bs4 snkrs early link test.py", line 30, in <module>
productid = pid[0]['content']
IndexError: list index out of range```
same again, runs the first 2, then fails
i only see one item with that specified name though
wydm?
you highlighted only one tag with that name
that tag is on every upcoming shoe
are you sure, every time you're iterating over these tags, somewhjere deep inside nested somewhere is a tag with this attribute
do you know how to debug?
i advise you simply print it out what you're geting
an empty listcannot be indexed
basically just print out what you're expecting
so type out what i want the results to be?
for item in items:
print(item)
inspect it here
pid = item.find(whatever)
basically if you're going over items, check if the item is what you expect itt is, before you try to extract something from it, that isnt' there, or is different in some way
scraping has lots of suprises like this
okay so
im just running this now:
soup = bs4.BeautifulSoup(res.text, 'lxml')
product = soup.find_all('a', attrs={'data-qa':'product-card-link'})
for p in product:
product_title = p.attrs['aria-label']
print(product_title)
product_link = p.attrs['href']
product_link = ('https://www.nike.com'+product_link)
print(product_link)```
and it gives me the results i want
https://www.nike.com/launch/t/air-jordan-1-satin-red
Air Jordan 1 Retro High OG CO.JP 'Tokyo' Release Date
https://www.nike.com/launch/t/air-jordan-1-retro-hi-tokyo
PG4 x Gatorade 'GX Orange' Release Date
https://www.nike.com/launch/t/pg4-gatorade-gx-orange
Air Jordan 6 'Quai 54' Release Date
https://www.nike.com/launch/t/air-jordan-6-quai-54-8-1
Air Jordan 6 'Quai 54' Release Date
https://www.nike.com/launch/t/big-kids-air-jordan-6-quai-54
Adapt BB 2.0 'Tie Dye' Release Date
https://www.nike.com/launch/t/adapt-bb-2-0-tie-dye
Air Jordan 1 Zoom 'Zen Green' Release Date
https://www.nike.com/launch/t/air-jordan-1-zoom-zen-green
Air Presto 'Australia' Release Date
https://www.nike.com/launch/t/air-presto-australia
Blazer Mid '77 'Grey Fog' Release Date
https://www.nike.com/launch/t/blazer-mid-77-grey-fog```
but i need the pid for each shoe. so im just showing that each shoe is there, and each shoe has the same 'name' tag
git and pip are unrelated
find a help channel, this isn't really webdev
you probably don't have git installed..?
download git bash
I need it for web development
k
try to fiddle around with it, im sure you'll get it @hollow kestrel
i'll try, i feel like imma be on this for ages
Sanic combined with Jinja2
An example I'm looking at contains <link rel="stylesheet" href="/static/style.css" /> which is essentially what I do
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>test test yeet</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,400i,700,700i,600,600i">
<link rel="stylesheet" href="/assets/fonts/fontawesome-all.min.css">
<link rel="stylesheet" href="/assets/fonts/simple-line-icons.min.css">
</head>
but not a single .css file is loaded. Thing is, I believe it would make more sense if my .js files loaded at the bottom of body
<script src="/assets/js/max.min.js"></script>
<script src="/assets/js/theme.js"></script>
<script src="/assets/js/cards.js"></script>
</body>
What are the possible causes of this?
the browser wasnt even sending a req for them?
Apparently 
you got a test site running?
Yup, hosted on 127.0.0.1 atm
My backend currently looks like this
import asyncio
from jinja2 import Environment, FileSystemLoader, Template
from sanic import Sanic
from sanic.request import Request
from sanic.response import html
app = Sanic(__name__)
env = Environment(loader=FileSystemLoader(searchpath=""))
template: Template = env.get_template("index.html")
render_dict = {
"test": "not an actual template yet"
}
@app.route("/", methods=["GET"])
async def test(request: Request):
await asyncio.sleep(1)
return html(template.render(render_dict))
app.static("/assets", "./assets/")
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000, debug=True)```
are you able to bind it to 0.0.0.0 and DM me the link?
btw Jinja has async rendering support
which will help be less blocking on the site further in
Let me push it to my VPS then 😂
you can DM me a zip with everything if thats easier, idk how much extra stuff you have inthere
Not much, don't worry
Issue fixed
Can i ask about bs4 and webscraping here?
If so, how do i get the text in an h3?
Like, for example, how do i get "Hey" from <h3>Hey</h3> after ive done this: variable = page_soup("h3",{"class":"Entry-title-1KT5j_Cm direkte-widget-title"}).
Hi ! I'm looking for remote user auth implementation in django and i don't find any tutorial, documentation (except the django's little one) or something like this. Anyone could help ?
i'm looking for an external authentification in django and i'm really lost ^^'
Hello everyone. I want to make a video streaming service for practice, but I read that django doesn't handle this type of job very well. So as a solution I decided to use JS, but I don't know how to integrate it. While django handles every other thing, I just want js to stream video. How can I do that?
Anyone here used JS scrapper before?
look into django auth
Hi guys i have a problem when trying to make a url redirect for a dynamic url in flask
is there anyone that could help me?
??
Pasting large amounts of code
If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/
After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.
Ok
@quick cargo
I forgot to add
the error is this:
rate = Rates.query.filter_by(id=id).all(
thats whats highlighter
and yeh i missed a bracket in this discord text
its .all()
AttributeError: 'builtin_function_or_method' object has no attribute 'translate'
i just want the url to take the user to the update page to edit that specific rate
yh
hmmm
oh
ik why
you do id=id but havent set id first
and by default id is a built in function
it would be a good idea to put id_ instead of id for your update section
but to get id from update to convert you'd need to pass it to convert via url or something
how do i do that
you'd need a similar think like '/convert/<id>' like you did for update
im confused
convert is the page that displays the information from the mysql table basically, can i not just have a link that directs it to update/(the id for the rate)?
@quick cargo
when i added variables to jinja template like list[server]['name'] it didnt start showing up
@cyan violet wdym
@native tide atm your issue is your referencing something that doesnt exist
you just happen to have called your var id which is a built in which is why it doesnt give you a undefined error
if i made convert/<id> wouldnt that do the same job as update/<id>?
well i did and for loop in html where it uses stuff like list[server]['name'] the things that i looped over didnt start showing up
@native tide pretty much yeah, but atm you dont actually have a id so you might aswell be just get everything
Im not sure entirely what your goal is but yh
@cyan violet dics with jinja are dict.server.name using . not []
the entire goal is, on newrates page i have a form to input information to store in the mysql db, that information is displayed on convert.html but i want a button underneath each rate to take the user to /update/<id> so they can edit and make changes to the rate
they you probably need to have a id section in the url in convert to get the Id specifically
otherwise you'll have to send everything in the table
i tried id with convert
but when i go to /convert i get Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
it still doesnt show up
if i do convert/6 (6 being a id) i get an error
you would need to redirect it with the id being part of the redirect
e.g redirect('conver/12344')
you should also change id to id_ to avoid shadowing inbuilts
i want convert to show everythign from the table and just a simple ubtton to redirect tp update/(id of the rate that the button was clicked on)
this is really confusing and frustrating
the trouble is you need the id to filter the results in this case
if you're doing it per user
ig? you could do it on a session basis
but that could have unintended concequences
@app.route("/update/<id>", methods=["GET", "POST"])
@login_required
def update_rate(id):
form = UpdateRatesForm()
rate = Rates.query.filter_by(id=id).first()
if form.validate_on_submit():
rate.base_currency=form.base_currency.data
rate.new_currency=form.new_currency.data
rate.bid_rate=form.bid_rate.data
rate.ask_rate=form.ask_rate.data
db.session.commit()
return redirect(url_for('convert'))
elif request.method == 'GET':
form.base_currency.data=rate.base_currency
form.new_currency.data=rate.new_currency
form.bid_rate.data=rate.bid_rate
form.ask_rate.data=rate.ask_rate
return render_template('update.html', title='Manage Rate', form=form, rate=rate)```
this is update_rate the link for /update/<id> all i want is if a user clicks the href button on /convert.html to redirect them to here to edit that one rate, how do i do it per user instead of session
i tried /convert/<id> but thats really complex for me
at the top of your code
from flask import session
then before you redirect
do session["convert_id"] = id would be the easiest method
then in your convert section you could do session.get("convert_id", some_default_val)
can there not be a simple <a href> to redirect it to that? idk man >>>>.<<<<
it has nothing todo with the redirect
this is just a issue of moving data around between endpoints
db.session.commit()
session["convert_id"] = id # We can set the id to the session here
return redirect(url_for('convert'))```
some_default = 1234
rate = Rates.query.filter_by(id=session.get("convert_id", some_default)).all()```
isnt some_default static?
can i use something like that
Description is a string ["description"]
list.server.description
and server is a variable
yeah, but it doesnt matter because that would just be a default id. Idk if alchemy supports None as a id filter
if it does then you dont even need the default value
what happens if the user finishes session
ill have to find an alternative to this
this is too compelx
May I post a question or would I be interrupting?
okay
I'm struggling with a NoReverseMatch and none of the answers on StackOverflow work for me
I have this line inside home.html:
{% extends "aggregator/base.html" %}
{% block content %}
{% for article in articles %}
<h2><a class="article-title" href="{% url 'Article-detail' article.link %}">{{ article.title }}</a></h2>
{% endfor %}
{% endblock content %}
that's causing the error, and I can't understand why.
Here's my ArticleDetailView class from views.py
class ArticleDetailView(DetailView):
model = Article
context_object_name = "article"
template_name = "aggregator/Article_detail.html"
Here's my url patterns list
urlpatterns = [
path("Article/<str:pk>", ArticleDetailView.as_view(), name="Article-detail"),
]
Also, this is how my model is:
class Article(models.Model):
title = models.CharField(max_length=100)
link = models.CharField(max_length=500, primary_key=True)
def get_absolute_url(self):
return reverse("Article-detail", kwargs={"pk": self.pk})
My entire error is as follows:
NoReverseMatch at /
Reverse for 'Article-detail' with keyword arguments '{'pk': 'https://techcrunch.com/2020/08/07/facebook-extends-coronavirus-work-from-home-policy-until-july-2021/'}' not found. 1 pattern(s) tried: ['Article/(?P<pk>[^/]+)$']
(Yes, I'm storing the techcrunch link as an Attribute of my Article model)
So what am I doing wrong?
Has anyone here worked with Cheerio before?
@native tide Read this for some more info on sessions. You can also use Flask-Login for sessions. https://coddyschool.com/upload/Flask_Web_Development_Developing.pdf
Guys how do you come up with web dev ideas?
screwing around with things
I see
def save_all(apps, schema_editor):
user_model = apps.get_model(*settings.AUTH_USER_MODEL.split('.'))
for user in user_model.objects.all():
user.save()
Would this work?
Also when defining the user model, why is there the split?
Django @glass sandal
Oh
This should be right if django doesn't have dataBase.commit()
It must be right actually
why would you do it like that though
I dont know other ways, I have existing users, and I need to create a new Profile instance for the old users, this seems like this would be a good option with a receiver
@flint breach
How about a data migration?
Or I could just do it a easier way
How could I use it in my case? Havent used data migration before
But isnt RunPython data migration?
Yea
That is the forward function for it
oh my bad, you're already runing one, silly of me
how come you're importing it like this, why not just directly import the user model you're using
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile_when_user_created(sender, instance, created, **kwargs):
if not instance.profile:
Profile.objects.create(user=instance)
I guess I could put this logic inside the save function
I foudn this in some documentation, about accesint the AUTH_USER_MODEL like that, but how could I directly access the user modek django provides?
from django.contrib.auth.models import User if you're just using the default user model
Isnt there something about that type of importing because you cant just impirt the midel like that in a migration? @flint breach
again, you're right, you need to import the model historically
apps.get_model('auth', 'user') perhaps?
Maybe, but the way it is now seems to also work just a bit differently, I am guessing that in this case it wouldnt make much of a difference as it is a one time use case so no reason for it to be dynamic
Is there any HTML, CSS editor that works with Django?
pycharm/vscode
I mean only code blocks and variables
Most IDEs do @hazy dove
Im using PyCharm Community and it doesnt have things like HTML Output (How page will looks like) etc.
it does, you just have to run it ... there isn't a lot an IDE can do to show you that unless your server is running
actually, pycharm community doesn't have it
community can use the runner... I'm pretty sure that is all the pro version does ... it just does it for you
oh wait, i thought you meant html highlighting
oh, you just want a local server integration?
Yes
oh, nevermind then 😄
Ok. Forget about Django. Just good HTML editor, only HTML editor
again, any IDE (vscode comes to mind first)
Any free alternatives?
vscode is free...
So only IDE, no external software
Yes, simple editor. Something like site-builder but without any special functions. Clear HTML and CSS
Not a full IDE
well ... maybe Brackets or Bluefish ... hell, even Notepad++ or something of the sort can do that
but VSCode, as far as IDE's are concerned... is pretty basic
you don't HAVE to install all the extensions that make it do a lot of fancy things
Ok
Is there any way I can paginate API data? Using flask btw
Not using any database, just pure API data, that I need to paginate
i think you would have to implement that yourself using some kind of class, here's how django does it with an example: https://docs.djangoproject.com/en/3.0/ref/paginator/#django.core.paginator.Paginator
ex/ https://docs.djangoproject.com/en/3.0/topics/pagination/
hey everyone! I'm trying to deploy my first django+react app. Does anyone have any recommendations/resources to deploy it? My react app communicates with django using the django rest framework
What's a good framework for designing micro services, apart from Django or Flask
Can someone explain in amazing detail how the meta class works within a form class in django?
Can someone explain in amazing detail how the meta class works within a form class in django?
@native tide the fields you define in the form class tell Django what values that form needs
the Meta class contains extra information about those fields, or the form itself.
Quick question, if I'm wanting to implement a fully fledged search, would I want to integrate elasticsearch with my mongodb?
Quick question, if I'm wanting to implement a fully fledged search, would I want to integrate elasticsearch with my mongodb?
@pale radish depends on what you want to search for and what what you're searching through
I assume you know it's for text
so it depends on the complexity of your queries.
and if it's for business you need to appreciate the tradeoff (increased complexity)
It's for a personal project I plan on hosting eventually
I'll have to think/research deeper about it and come back
Was checking if it was an easy yes or no, I currently just have full-text search implemented in mongodb
it would really depend on the complexity of your search
basically
the more complex your queries
the more likely that Elasticsearch will perform better
My data is pretty well correlated to one another, so I don't think any of my queries will be too complex
Maybe it's something I implement when I have to
I need help, so basically I have a form which purpose is to post info to the server and the server responses with the same page just updated with the info but the thing that changes is the url and that form's action is pasted onto the url, how can I prevent that from happening?
Please someone respond to me this is super critical to me right now.
def login(request): if request.method == 'GET': return render(request, 'login/login.html', {'form':UserCreationForm}) else: if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid: user = form.save()
At the very bottom of this django form where I form.save() . I'm saving it into that user variable. Is that variable specificly related to the django USER model
Any django god out there please sos
@naive gustGod
Is the variable named user arbitrary or is it reffering to the user model
tytyt
You figured it out?
I recommend using built in user authentication
if your in a rush
How do I do that one
I want the easiest way. I'm just learning and trying to simply get it done
oh I think you're talking about with the templating language? user.authenticate
brb movie
How can you backreference a OneToOne relationship in django?
Total noob here, how can I use a godaddy domain to redirect to my website made with flask?
I'm using Flask with render_template to render html/css/javascript for my GUI. I'm confused as to how I'm supposed to execute native Python functions with a render template since the render template only lets me write javascript functions to be executed within the app/website and not to my server/client.
Here is my flask code so far:
from flask import Flask, render_template, request
from flaskwebgui import FlaskUI
app = Flask(__name__)
ui = FlaskUI(app)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/showSettings")
def showSettings():
return print("Test text")
ui.run()```
I want to be able to click a button on my website and have it pull user preferences from a `prefs.json` file, although that's not possible if all I have access to are the JS functions within the website/app.
How can you backreference a OneToOne relationship in django?
@limber laurel the name of the other model, by default
Ok, thanks
So if I do user.profile it should work? Because when I tried something like that durning a data migration I got user has no attribute profile
does the user have a profile?
I'm assuming you have something like:
class Profile(models.Model):
user = models.OneToOneField('User')
guys, can i use django as backend and pair it with css, html and js on frontend?
yes
ok, thx for the info
Anyone in here knows how to connect postgresql db to flask app (and to create it)
do u know if it's posible to run django on apache
ok, thx for the info
@uncut rover you probably want DRF for that
django-rest-framework
To connect frontend with django backend?
i get this when i run pytest
i have all the imports on the script
and in the VM
@native tide have you pip installed flask
yes
after removing it from requirements.txt and running it again i get:
est/test_back_end.py:4: in <module>
from flask_testing import TestCase
E ImportError: No module named flask_testing
but for that
Requirement already satisfied: flask_testing in ./venv/lib/python3.6/site-packages
Requirement already satisfied: Flask in ./venv/lib/python3.6/site-packages (from flask_testing)
Requirement already satisfied: Jinja2>=2.10.1 in ./venv/lib/python3.6/site-packages (from Flask->flask_testing)
Requirement already satisfied: itsdangerous>=0.24 in ./venv/lib/python3.6/site-packages (from Flask->flask_testing)
Requirement already satisfied: click>=5.1 in ./venv/lib/python3.6/site-packages (from Flask->flask_testing)
Requirement already satisfied: Werkzeug>=0.15 in ./venv/lib/python3.6/site-packages (from Flask->flask_testing)
Requirement already satisfied: MarkupSafe>=0.23 in ./venv/lib/python3.6/site-packages (from Jinja2>=2.10.1->Flask->flask_testing)
requirement is satisfied
@native tide
Sorry man I don't really know, I'm not too experienced
why an i getting this error
and look at the code, maybe something is wrong with it, idk
class Product(models.Model):
title = models.TextField()
desc = models.TextField()
price = models.TextField()
summary = models.TextField(default='this is good')
maybe install flask_test
tried it
same issue
Do sessions carry over if the user closes the tab?
this is the test file
import unittest
from flask import url_for
from flask_testing import TestCase
from application import app, db, bcrypt
from application.models import Users, Rates
from os import getenv
class TestBase(TestCase):
def create_app(self):
# pass in configurations for test database
config_name = 'testing'
app.config.update(SQLALCHEMY_DATABASE_URI=getenv('TEST_DB_URI'),
SECRET_KEY=getenv('TEST_SECRET_KEY'),
WTF_CSRF_ENABLED=False,
DEBUG=True
)
return app
def setUp(self):
"""
Will be called before every test
"""
# ensure there is no data in the test database when the test starts
db.session.commit()
db.drop_all()
db.create_all()
# create test admin user
hashed_pw = bcrypt.generate_password_hash('admin2016')
admin = Users(first_name="admin", last_name="admin", email="admin@admin.com", password=hashed_pw)
# create test non-admin user
hashed_pw_2 = bcrypt.generate_password_hash('test2016')
employee = Users(first_name="test", last_name="user", email="test@user.com", password=hashed_pw_2)
# save users to database
db.session.add(admin)
db.session.add(employee)
db.session.commit()
def tearDown(self):
"""
Will be called after every test
"""
db.session.remove()
db.drop_all()
class TestViews(TestBase):
def test_homepage_view(self):
"""
Test that homepage is accessible without login
"""
response = self.client.get(url_for('home'))
self.assertEqual(response.status_code, 200)
Say we had a row in SQLAlchemy DB...
user1 = User(email='x', password='y')
How could I retrieve the ID of user1 (primary key)?
idk
Does anyone have an idea of my error?
I need help, so basically I have a form which purpose is to post info to the server and the server responses with the same page just updated with the info but the thing that changes is the url and that form's action is pasted onto the url, how can I prevent that from happening?
django.core.exceptions.ValidationError: ['“1st bool field” value must be either True or False.']
what is this error???!!!!
so i know this is irrelevant but can someone suggest a nice webdesign for 6 cards that have unequal text .i tried stacking em 3 in in 1 row and 3 in next but it looks bad
please can anyone help me if so please ping with a answer
please
can someone please help me out with this pytest error im getting im really desperate now
@left vigil is it a GET or POST request
POST
Flask or django? @left vigil
do you have methods = ['GET','POST',] in the app route
if it changes the uri itself, im gueesing you're still passing some sort of arguments via a get method
"your own http"?
im guessing he means framework and server
@quick cargo what code
@flint breach i have a form who's action is set to /card
and after the form is submitted the url changes to that url
but i dont want it cuz it doesnt suppose to redirect
its long
well, if you set it to an action, it sends a request there
you want to send a request, but to remain on the page?
aka, you don't want the page to refresh?
then link a github or send a paste bin
we cant really guess what you have
but not chagne the url
or dont have
@quick cargo it's long code but contains private info ill change it and upload
then you have to redirect back to the place where you sent the request from
how?
lucky for you, built into the http protocol is the http_referer flag
well, if you're making your own framework, that implementation detail is up to you
if you used Django or flask the answer would be simple 🤷♂️
after the form is submitted is it up to the server to refresh the page or is it client side
client
well technically its a redirect the server sends back
and the client follows the redirect therefore reloading the page
well we dont know you code so we cant really help much
okkk ill upload it soon
<details>
<summary><a href="{% url 'page2'%}">List</a></summary>
<ul style="list-style-type:none;">
<li>1</li>
<li>2</li>
</ul>
</details>
hi,
I'm making a website and in my base html file, there is a details-summary element.
The problem i have is that when i want to click "List", i want to both show the elements of the list below and go to another page at the same time and it only goes to the link in the a tag.
The "page2" inherits the base file so there is going to be that details-summary element there too.
anyone has good tutorial on flask socket io, i have forum but i want to allow users to chat in private
I guess documentation is sufficient for your needs @fickle fox
I mean the official on
one*
Does anyone have any idea on how to update an existing client's card on stripe? Their api only covers session based update but I am looking to update it through my code. Basically a frontend modal posting to the backend which should utilize the stripe api endpoints
I have talked with their support they "will talk with their dev team and give me a solution"
Asking here maybe someone has a quick fix
i am trying qwith it tho kinda confusing
I see
Guys I hate frontend dev and mostly CSS . What could I do to avoid it as much as I can?
Does anyone know how I can make a {% if %} condition, refering to a class template, in django?
Guys I hate frontend dev and mostly CSS . What could I do to avoid it as much as I can?
@glass sandal Bootstrap is a good alternative for you, but remember a lot of people use this framework, so your design will be similar to a lot of websites
I see
@formal shell And you can pass it as a refrenc
refrence*
Like
return render(request,"site.html",{"class":Class})
use bootstrap?
Fair enough @glass sandal
Do you guys think this is a good course to learn Flask for a beginner? https://www.udemy.com/share/101zE4AkQZcVxaQw==/
Don't waste money in Udemy @vernal vault
Youtube tutorials are sufficient
Or even the documentation
I'm doing one now and learning a ton I scraped page this morning
for 10 its not a big risk
You could've done this all with some yt vids
who would you recommend for Youtube
Or even documentation
I just found tech with tim
He has good tutos
And very great for beginners
But I myself rather getting used to documentations
So like for flask : https://flask.palletsprojects.com/en/1.1.x/installation/
I feel like I need to move to the next step get better working with objects from libraries
wdym by objects? classes?
yes and how they work in libraries
Oh
and getting better with docs
Well , a good site to learn basics of programming could be sololearn.com
And again , youtube tutorials
yw
that guy who made that course in on youtube as well 🙂 https://www.youtube.com/channel/UC-QDfvrRIDB6F0bIO4I4HkQ
best tutorial for it hands down is corey
In this Python Flask Tutorial, we will be learning how to get started using the Flask framework. We will install the necessary packages and get a basic Hello World Application running in our browser. Let's get started...
The code for this series can be found at:
https://githu...
Goes over everything from getting started to logins to deployment
@teal fox We don't allow recruitement/unapproved advertising
So forms are considered advertising?
There is no personal info asked of anykind
Not even emails
Just answers, since I would like to study what people think of affiliate marketing
Answers are locked to "yes/no" basis with X amount of difference choices to choose from
It's still recruitement
You're asking people to answer a form for you
Its not a forum, its a form. Google form
No personal data is collected
You or I cant contact the person or know who they are or how they answered
I can only see the big picture that X amount of people answered Y and Z amount of people answered U
Yes, but how is it recruitement?
Are we not allowed to ask people to answer form that has no personal or other personal data collected? Only an opinion?
It's advertising something @teal fox, there are other places for that (see: reddit)
Sorry, im having hard time understanding what im advertising?
Listen, the mods have spoken. You can accept it, or not, but the conversation is over.
Advertising means it would have monetary value for me, but it really doesnt generate any revenue for me or anyone else (except probably Google, but they get that anyways since they follow everything you do online if you use Chrome as browser)
Take it up in meta if you have disagreements
@mellow tide saying "mods has spoken" blaablaablaa is the most stupid argument you could have come up with. Plus I was talking to Xithrius, so I can understand better, to ensure the rules are followed by me, better next time.
First rule to learning is understanding
And as I pointed out I've still not come to any conclusion on why or how I broke the rules, since there was no advertising. Simple a request for people (anyone who wants) to answer a questionaire that generates no revenue or requests any personal info.
And you are making excuses, and spamming the chat aswell 😉
Guys does anyone have an idea for a good tutor for django 3?
@teal fox To discuss this further and come to a conclusion that both of us can agree on, I'd like you to message @hexed spoke
You already deleted it so no need to continue the talk. You can PM me for better explanation if you have the time and energy.
I still have the deleted message.
@uncut rover have you tried googling online courses or YouTube for that topic? There is usually a lot of good open courses / videos on youtube on almost any topic.
Using modmail will allow you to talk to multiple staff members. We can work this out if you'd like.
@uncut rover The official django doc has a very good tutorial that'll guide you to make a poll app, it should give you enough material to understand most "basic" things
https://www.youtube.com/watch?v=xv_bwpA_aEA&list=PL-51WBLyFTg2vW-_6XBoUpE7vpmoR3ztO is a django 3 tutorial course. I do not know if it's any good, but the titles of the videos in the playlist seem to cover a lot of the basics so it might be a good starting point
No, I have not followed that course. It was the first result when i searched youtube for 'django 3 tutorial'. Given django 3.1 was released 4 days ago I highly doubt someone has created a complete tutorial for that version by now. I have not verified this, but I would not expect there to be major breaking changes in 3.1 that would affect an introductory tutorial.