#web-development

2 messages · Page 76 of 1

crystal quartz
#

I prefer the latter because it is (a) easily manageable from the web and (b) it survives any server moves

rustic pebble
#

Thats what I did

#

the latter one

#

Just finished setting it up

crystal quartz
#

oh, well, cool

rustic pebble
#

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?

brave elbow
#

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.

warm igloo
cold anchor
#

@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)

brave elbow
#

@warm igloo right on! thank you!

warm igloo
#

Welcome. I am an average programmer even after 20 years. But I'm a helluva Googler!

#

😄

noble star
#

Googler == programmer. Change my mind

native tide
#

@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?

cold anchor
#

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

native tide
#

I'll look further into this, thank you for your time. 👍

cold anchor
#

happy to help!

wind walrus
#

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?

wicked tide
#

Is there a way to synchronize column width between multiple tables?

native tide
#

@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

pseudo marsh
#
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

queen sinew
#

can't you just add another field under email = at the class level?

pseudo marsh
#

yes, but I do not know how to make it a dropdown thing, and also mark it as a non-mandatory field

pseudo marsh
#

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?

queen sinew
#

it's been a while since i've used django but i believe all field types take an initial value

pseudo marsh
#

Thank you for pointing those out! I think I have what I need now ! Thanks

queen sinew
#

no problem 🙂

solar hatch
#
@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")
})
clear bobcat
#

I dont know y i m blocked from the projects page but ya this is my project
Please do like it on fb

polar nova
#

is there anyone who use twilio

rigid laurel
#

probably

polar nova
#

did you upgrade it

uncut rover
#

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

sage spear
cold anchor
#

use .herokuapp.com instead of .heroku.com

native tide
#

there is no ssl

uncut rover
#

umm, anyoe has any idea of how to solve my coe issue?

#

code**?

sage spear
#

@cold anchor thx

cold anchor
#

your form is malformed @uncut rover

uncut rover
#

wdym?

pulsar ivy
#

Try using loginmanager its much easier

uncut rover
#

im doing a tech with tim tutorial and im following his instructions

cold anchor
#

what's in base.html ?

uncut rover
#

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**

cold anchor
#

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' ?

uncut rover
#

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>
fierce frost
#

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
cold anchor
#

why do you have 2 separate forms @uncut rover ?

uncut rover
#

I dont

#

Idk

#

Im using tech with tim to learn flask, and he did 2 forms, so i did the same

cold anchor
#

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

uncut rover
#

SO you say i need to put the email in the same form?

cold anchor
#

no, that's not what I'm saying

#

do you know why you're getting the error that you're getting?

uncut rover
#

Because it thinks im activating the 'n' keyword and not email

#

thats the error, right?

cold anchor
#

correct, you are accessing 'n' and not 'email'

#

in the third line of you login() view

uncut rover
#

ok, im going to my grandparents rn, ill be in 2hrs back

#

well continue in 2hrs, im going to gp's house

cold anchor
#

I'll be at work, won't be available

native monolith
#

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?

late fjord
#

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

flint breach
#

im at work rn

late fjord
#

@native tide

#

help

uncut rover
#

nvm it works

marsh canyon
#

@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

uncut rover
#

@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

elfin peak
#

Is anyone available to help me with a quick issue?

valid cypress
elfin peak
#

idk i suck at web development are you using django?

valid cypress
#

Yes, Django as backend, because this is Code Jam project

elfin peak
#

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 ^^

queen sinew
#

the slash going outside of the name

#

"/<name>"

uncut rover
#

@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)
native tide
#

@elfin peak ```py
@app.route("</name>")

should probably be "/<name>"
uncut rover
#

i changed it

#

i noticed it

elfin peak
#

Ok

#

wait no because it worked befoire

uncut rover
#

Oh, im just recommending a better way, to make your code more readable

elfin peak
#

ill keep that in mind thanks

#

Also @uncut rover the code u sent me works

#

so thanks

uncut rover
#

np

elfin peak
#

I just needed it to work so I can keep learning lol

uncut rover
#

tech with tim?

elfin peak
#

yesir

uncut rover
#

me too

elfin peak
#

cant chat playing rocket league srry

#

I have this scrrenn on other monitor tho

native tide
#

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.

native tide
#

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

fickle star
#

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

#

It throws the above error. However, nothing seems to be wrong here.

#

Any suggestions please

cold anchor
#

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)

lavish prismBOT
#

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:

https://paste.pythondiscord.com

zealous cloud
#

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

elfin ridge
#

I think you need to loop through the json object

zealous cloud
#

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?

elfin ridge
#

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 %}

fickle star
native tide
#

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.

fickle star
#

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.

native tide
#

I'm glad! No worries.

zealous cloud
#

@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

solemn thistle
elfin ridge
#

@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.

zealous cloud
#

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

elfin ridge
#

Ok, glad it works! No probs

native tide
#

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)

cold anchor
#

you ready to use javascript? because you'll need to use javascript

native tide
#

absolutely!

#

that's actually what I was hoping for, just lost on how to connect the two without a reload/change.

cold anchor
#

so the most standard way is to send a request to your API from javascript and show the error element if/when it fails

native tide
#

would you happen to be able to link an example for that process, or a resource I could look to?

cold anchor
native tide
#

That's wonderful, thank you very much!

dapper raft
#

How to make website like hastebin?

#

Plz ping when anyone reply

snow dragon
#

Yeah your nickname makes pinging impossible. Sorry

#

@dapper raft

dapper raft
#

Ty

#

@snow dragon I wnt in py lang

#

🙂

snow dragon
#

@dapper raft Well it's not written in Python. You can use that code as a framework to build your own.

dapper raft
#

Hmmmmm

alpine tinsel
#

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
proper osprey
#

In Twitter, is it possible to see traces of APIs in the source page?

valid cypress
#

How to validate Email format in DRF serializer?

elfin ridge
#

@valid cypress you can set the serializer field as EmailField e.g. email = serializers.EmailField()

elder nebula
#

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

elfin ridge
#

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.

wicked tide
#

@alpine tinsel How does it 'not work'?

flint breach
#

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

uncut rover
#

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
elfin ridge
#

@uncut rover can you share the exact exception your receiving?

uncut rover
#

it says that those properties for db dont exist

#

Column, string and integer

#

@elfin ridge ^

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

uncut rover
#

Yeah, did that

#
from flask_sqlalchemy import SQLAlchemy
#

can i do maybe from SQLAlchepy import Column, String integer?

#

@elfin ridge

#

^

elfin ridge
#

I'm not sure - have you defined 'app' correctly above?

#

e.g. app = Flask(__name__)

uncut rover
#

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

elfin ridge
#

have you provided a sqlite database uri? e.g. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

native tide
#

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

uncut rover
#

yeah

#

and ive provided a database

#

maybe thats whats wrong, tell me

native tide
#

Can you paste that class in and see if it works

uncut rover
#
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

native tide
#

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

uncut rover
#

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

native tide
#

did you do pip install flask_sqlalchemy in the terminal

uncut rover
#

no

native tide
#

and imported it

from flask_sqlalchemy import SQLAlchemy

#

do that

uncut rover
#

actually, i did install it

#

whats the issue

#

?

native tide
#

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

uncut rover
#

i did do that, and it didnt work

elfin ridge
#

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

uncut rover
#

yeah, ill send you all the code

#

then it will give you a better understanding

native tide
#

Did you create the table after setting it up?

In the python terminal from [filename] import db
then
db.create_all()

uncut rover
#

ill do it in hastebin

#

here is the code

native tide
#

Line 77, I'm not fully sure but I thought db.create_all() can only happen from the shell

uncut rover
#

wdym?

native tide
#

Actually i think im wrong

#

Just saw an example

uncut rover
#

the error was before i did db.create_all()

native tide
#

The class looks right to me, have you tried to remove

        self.name = name
        self.email = email

and see if it works?

lucid vine
#

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?

crystal stirrup
uncut rover
#

so does anyone have a solution?

native tide
#

guys i'm totally new, what's the main difference between Django and Flask?

vestal hound
#

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

native tide
#

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 😃

vestal hound
#

if you have a separate frontend

#

you might wanna look into DRF

#

django-rest-framework

untold flare
#

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.

crystal quartz
#

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>
lucid vine
#
@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

kind steppe
#

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

https://blog.jetbrains.com/pycharm/2020/08/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 to our

flint breach
#

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.
@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

crystal stirrup
#

instead of just 1 line

bleak bobcat
#

I'm guessing you wouldn't call the summary but something else...Check their documentation

crystal stirrup
native monolith
#

can I make in Flask simple gui for text input and few buttons ?

elfin ridge
#

this is what it looks like it and has loads more
@crystal stirrup have you tried scrolling along to the right?

crystal stirrup
#

🤦‍♂️ im new to coding and didn't notice that small scroll

elfin ridge
#

haha - no worries - easy mistake, it doesn't wrap the text

native tide
#

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?

languid shard
#

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?

native tide
#

Are you using FlaskForm @languid shard

#

And have you set your app.config['SECRET_KEY'] to something

languid shard
#
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
elfin ridge
#

@languid shard you may need to set your server name in your config.py e.g.

SERVER_NAME = "yourapp.heroku.com"
languid shard
#

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

elfin ridge
#

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.

polar wasp
#

@native tide it's unusual for user id's to be secret, anyway

elfin ridge
#

I had deprecated csrfprotection syntax
@languid shard glad you found the issue!

languid shard
#

Unfortunately, as it often is, I found another issue 😦

polar wasp
#

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

languid shard
#

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>
elfin ridge
#
# 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

languid shard
#

I changed it

#

I deleted that line

elfin ridge
#

Ah ok!

languid shard
#

I used to do that, so maybe...?

elfin ridge
#

You mentioned this is an issue when running on Heroku, so I assume you don't have this issue when running locally?

languid shard
#

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

elfin ridge
#

@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?

languid shard
#

Sorry, forgot to try it 😊

#

trying it rn

elfin ridge
#

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.

languid shard
#

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

elfin ridge
#

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

languid shard
#

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

elfin ridge
#

What error are you getting now? I've lost track

languid shard
#

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

#

Registering as a manager doesn't work for me

elfin ridge
#

I registered as manager fine, logging in however I was met with a 400 error

languid shard
#

that's weird

elfin ridge
#

is there anything in the logs which reveal the issue a bit more?

languid shard
#

Not that I can see

elfin ridge
#

there's only one dyno running for the service as well I assume?

languid shard
#

Yes

elfin ridge
#

Managed to login in as a manager fine now

languid shard
#

...?? I don't know what's happening

#

I changed the key after I pushed

#

I thought I put it into .gitignore though 🤷

elfin ridge
#

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

languid shard
#

hmmm

elfin ridge
#

You may need to set the SESSION_COOKIE_DOMAIN = "safe-bayou-60879.herokuapp.com" as well

languid shard
#

Pushing to heroku rn

#

I think it didn't work. One thing I'm noticing is that the apology isn't secure

lapis spear
#

yo i had reset my django database by deleting migration files and the sqllite file did i do it correctly?

languid shard
#

However, whenever it works it's always https secured

elfin ridge
#

It's only say it's not secure due to the image link is over http rather than https

lapis spear
#

yo how to reset the database in django (sqlite)

elfin ridge
#

yo how to reset the database in django (sqlite)
@lapis spear Delete database file and delete migration files (.py and .pyc) in migrations directory of your app (don't delete init.py file). Then run python manage.py makemigrations and python manage.py migrate.

lapis spear
#

@elfin ridge oh yes yes thanks sir

#

@elfin ridge btw is it the standard way of doing it?

elfin ridge
#

I think so, if you're looking to start afresh it's probably the cleanest way

lapis spear
#

@elfin ridge thank you again sir

elfin ridge
#

No probs

languid shard
#

@elfin ridge any advice for me moving forward?

rustic pebble
#

Has anyone deployed Quart to heroku before? I am getting the following error: ModuleNotFoundError: No module named 'quart.worker'

polar coral
#

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 😦

languid shard
#

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
wild scroll
#

Hey guys

#

Does anyone here work with HTML?

flint breach
#

probably most people, at least in some way

#

ask your question

wild scroll
#

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

elfin ridge
#

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

native tide
#

hey

#

is anyone here ?

#

can anyone explain what a "file path" is ?

languid shard
#

@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

native tide
#

nvmd found it

elfin ridge
#

@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.

languid shard
#

Same 🤷 It's frustrating

elfin ridge
#

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

languid shard
#

yeah that caused an internal 500 error

vast remnant
#

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

languid shard
#

@elfin ridge nvm that's something else

elfin ridge
#

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.

#

@elfin ridge nvm that's something else
@languid shard guessing it didn't work?

untold flare
#

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"

elfin ridge
#

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.

elfin gyro
#

--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.

#

2nd line, says, Processor temperature: None

#

If anyone know how to fix this pls @ me 😬

wintry flicker
#

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

nimble epoch
#

@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 %}

languid shard
#

@elfin ridge Sorry, got stuck in a meeting. It doesn't break it, but it doesn't fix it either

wintry flicker
#

@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

nimble epoch
#

@wintry flicker Did you change it to {% ... %}?

wintry flicker
#

What do you mean?

nimble epoch
#

Change your (% for users in database %) to {% for users in database %}

wintry flicker
#

ah

nimble epoch
#

And change it for endfor too

wintry flicker
#

Alright

#

it worked

#

Thank you

nimble epoch
#

👍😉

#

Yw

low blade
#

when hosting a discord bot, is it a good idea to allow IPv6? im not all that sure where to put this question lol

keen acorn
#

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

#

:^)

north aurora
#

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!

Medium

Make sure your Django application is ready to deploy and running well on localhost, run python manage.py runserver on terminal.

wind walrus
#

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?

marble pulsar
#

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

night spoke
#

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)

mild wind
#

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

pseudo marsh
#

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?

flint breach
#

posts is not a database, it's a table in your database

#

had to iron out that quirk hehe

#

so basically, you want pagination?

pseudo marsh
#

oh, I'm sorry about that

flint breach
#
from django.core.paginator import Paginator```
#

hehe

pseudo marsh
#

It's the only table in my database but yeah

#

okay, Pagination is what it's called

flint breach
#

basically yea, you list the first 10

pseudo marsh
#

Right, what then?

flint breach
#

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

pseudo marsh
#

oh, do you know of a tutorial that shows how I'd go about doing that?

#

ie, in code

flint breach
#

django docs 🤷‍♂️

pseudo marsh
#

the ajax thing as well?

flint breach
#

the pagination 100%, the ajax probably aswell, if not on django docs then on stack overflow or whatever

#

google will be your friend 🙂

pseudo marsh
#

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 !

flint breach
#

if you're doing anything that seems very common, django probably has a shortcut for it

#

pagination, generic views, etc etc

wind walrus
#

If I'm using S3

#

Do I replace all the Jinja2 {{url_for('static', filename='xxx')}} with s3 links?

coral wind
#

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

flint breach
#

"constant"?

#

javascript is clientside, what do you mean by stay on the server

coral wind
#

oh

#

so i cant do that with js?

flint breach
#

im unsure what you're trying to do

#

also getElementById is a function, you're calling it wrong afaik?

coral wind
#

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

flint breach
#

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

coral wind
#

i wanted to do the app with JS

#

bcs i want to host it on github pages

wanton ridge
#

anyone can explain me what a local webserver is?

wind walrus
#

Is it possible to have both a static folder for development localhost purposes and also use Amazon S3 link?

mild wind
#

@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

coral wind
#

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)

glass sandal
#

Guys I have a question : Does google's search engine have an API?

#

I want to make a website that shows search results from google , but with my design

#

Or even from duckduckgo

elfin ridge
glass sandal
#

I see

#

So only 100 requests?

#

And another question , I couldn't scrape the website itself

#

Like I use BeautifulSoup

elfin ridge
#

"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."

native tide
#

And another question , I couldn't scrape the website itself
@glass sandal against TOS

glass sandal
#

And when I do :

soup.select(".g")

It returns nothing

#

What is TOS?

#

Like it doesn't allow me to scrape it?

elfin ridge
#

TOS = terms of service

glass sandal
#

Oh

coral wind
#

i missed a semicolon at line 9...

glass sandal
#

So it is illegal?

#

wow

native tide
#

and you wont get help here either

glass sandal
#

I mean , I wouldn't share the website to influence from it tho

#

Like I want it for myself

#

I see

#

The rules...

elfin ridge
#

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

glass sandal
#

I see...

#

I use a VPN most of the time anyway

elfin ridge
#

It's a limit of 100 per a day, so 3,000 requests a month

glass sandal
#

Well, I guess I'll have to ask stackoverflow hen

#

then*

elfin ridge
#

You can ask here, I'm not sure why crazygmr101 is saying you won't get help here

glass sandal
#

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?

hollow kestrel
#

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

flint breach
#

find_all returns a list of instances not an instance itself, afaik

#

either pid[0]['content'] or soup.find.. instead of soup.findall

hollow kestrel
#

lemme try

#

first thing worked! thanks man

#

massively appreciated

hollow kestrel
#

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```
flint breach
#

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

hollow kestrel
#

would you know why? because it is there in the html

flint breach
#

name is branch:deeplink:productId?

hollow kestrel
#

yep

flint breach
#

care for a screenshot

hollow kestrel
#

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

flint breach
#

i only see one item with that specified name though

hollow kestrel
#

wydm?

flint breach
#

you highlighted only one tag with that name

hollow kestrel
#

that tag is on every upcoming shoe

flint breach
#

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

hollow kestrel
#

the same tag is on each shoe

#

and idk how to debug

flint breach
#

basically just print out what you're expecting

hollow kestrel
#

so type out what i want the results to be?

flint breach
#
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

hollow kestrel
#

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

upbeat ledge
#

How to clone git on windows ?

#

do I have to pip something first ?

flint breach
#

git and pip are unrelated

upbeat ledge
#

ok

#

but how to clone ?

#

git clone <repo>

#

is not working

#

for me in cmd line

flint breach
#

find a help channel, this isn't really webdev

#

you probably don't have git installed..?

#

download git bash

upbeat ledge
#

I need it for web development

flint breach
#

or something

#

unfortunatley gotta run for work

upbeat ledge
#

k

flint breach
#

try to fiddle around with it, im sure you'll get it @hollow kestrel

hollow kestrel
#

i'll try, i feel like imma be on this for ages

fierce frost
#

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?

quick cargo
#

hey shiv

#

In console are you getting 404 or 200 OK when loading

fierce frost
#

Neither

#

.css files aren't loaded at all

#

whereas .js files are loaded with 200

quick cargo
#

the browser wasnt even sending a req for them?

fierce frost
#

Apparently catshrug

quick cargo
#

you got a test site running?

fierce frost
#

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)```
quick cargo
#

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

fierce frost
#

Let me push it to my VPS then 😂

quick cargo
#

you can DM me a zip with everything if thats easier, idk how much extra stuff you have inthere

fierce frost
#

Not much, don't worry

Issue fixed

rigid mist
#

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"}).

cursive dirge
#

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 ?

flint breach
#

remote user auth?

#

you mean via rest?

cursive dirge
#

i'm looking for an external authentification in django and i'm really lost ^^'

arctic mist
#

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?

rough canopy
#

Anyone here used JS scrapper before?

flint breach
#

look into django auth

native tide
#

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?

#

??

quick cargo
#

@native tide put it in a past bin here

#

!paste

lavish prismBOT
#

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.

native tide
#

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

quick cargo
#

okay so its redirecting correctly

#

is this sqlalchemy

native tide
#

yh

quick cargo
#

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

native tide
#

how do i do that

quick cargo
#

you'd need a similar think like '/convert/<id>' like you did for update

native tide
#

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

cyan violet
#

when i added variables to jinja template like list[server]['name'] it didnt start showing up

quick cargo
#

@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

native tide
#

if i made convert/<id> wouldnt that do the same job as update/<id>?

cyan violet
#

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

quick cargo
#

@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 []

cyan violet
#

idk other times []

#

worked

#

but how can i get stuff in string

native tide
#

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

quick cargo
#

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

native tide
#

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.

cyan violet
#

it still doesnt show up

native tide
#

if i do convert/6 (6 being a id) i get an error

quick cargo
#

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

native tide
#

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

quick cargo
#

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

native tide
#
@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

quick cargo
#

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)

native tide
#

can there not be a simple <a href> to redirect it to that? idk man >>>>.<<<<

quick cargo
#

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()```
native tide
#

isnt some_default static?

cyan violet
#

can i use something like that

Description is a string ["description"]
list.server.description

#

and server is a variable

quick cargo
#

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

native tide
#

what happens if the user finishes session

#

ill have to find an alternative to this

#

this is too compelx

pseudo marsh
#

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?

rough canopy
#

Has anyone here worked with Cheerio before?

native tide
glass sandal
#

Guys how do you come up with web dev ideas?

quick cargo
#

screwing around with things

glass sandal
#

Hmmm

#

So you just test stuff out?

quick cargo
#

pretty much

#

See what works, see what doesnt

#

get a feel for colour matches

glass sandal
#

I see

limber laurel
#
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?

glass sandal
#

Is this flask or django?

#

@limber laurel

limber laurel
#

Django @glass sandal

glass sandal
#

Oh

#

This should be right if django doesn't have dataBase.commit()

#

It must be right actually

flint breach
#

why would you do it like that though

limber laurel
#

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

flint breach
#

How about a data migration?

limber laurel
#

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?

flint breach
#

Yea

limber laurel
#

That is the forward function for it

flint breach
#

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

limber laurel
#
@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?

flint breach
#

from django.contrib.auth.models import User if you're just using the default user model

limber laurel
#

Isnt there something about that type of importing because you cant just impirt the midel like that in a migration? @flint breach

flint breach
#

again, you're right, you need to import the model historically

#

apps.get_model('auth', 'user') perhaps?

limber laurel
#

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

hazy dove
#

Is there any HTML, CSS editor that works with Django?

flint breach
#

pycharm/vscode

hazy dove
#

I mean only code blocks and variables

mellow tide
#

Most IDEs do @hazy dove

hazy dove
#

Im using PyCharm Community and it doesnt have things like HTML Output (How page will looks like) etc.

mellow tide
#

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

flint breach
#

actually, pycharm community doesn't have it

mellow tide
#

community can use the runner... I'm pretty sure that is all the pro version does ... it just does it for you

flint breach
#

oh wait, i thought you meant html highlighting

#

oh, you just want a local server integration?

hazy dove
#

Yes

flint breach
#

oh, nevermind then 😄

hazy dove
#

Ok. Forget about Django. Just good HTML editor, only HTML editor

mellow tide
#

again, any IDE (vscode comes to mind first)

hazy dove
#

Any free alternatives?

mellow tide
#

vscode is free...

hazy dove
#

So only IDE, no external software

mellow tide
#

do what now?

#

what are you looking for ... you said editor

hazy dove
#

Yes, simple editor. Something like site-builder but without any special functions. Clear HTML and CSS

#

Not a full IDE

mellow tide
#

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

hazy dove
#

Ok

distant trout
#

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

mellow tide
#

yeah idk, i've never done it outside of sqlalchemy

#

i'm sure there is

mossy latch
dim belfry
#

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

native tide
#

What's a good framework for designing micro services, apart from Django or Flask

native tide
#

Can someone explain in amazing detail how the meta class works within a form class in django?

vestal hound
#

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.

pale radish
#

Quick question, if I'm wanting to implement a fully fledged search, would I want to integrate elasticsearch with my mongodb?

vestal hound
#

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)

pale radish
#

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

vestal hound
#

it would really depend on the complexity of your search

#

basically

#

the more complex your queries

#

the more likely that Elasticsearch will perform better

pale radish
#

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

vestal hound
#

if you're just doing basic stuff like "contains 'word'"

#

don't bother

pale radish
#

basically yes

#

Thanks for the help

left vigil
#

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?

native tide
#

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

wheat swift
#

You figured it out?

#

I recommend using built in user authentication

#

if your in a rush

native tide
#

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

limber laurel
#

How can you backreference a OneToOne relationship in django?

full nimbus
#

Total noob here, how can I use a godaddy domain to redirect to my website made with flask?

wheat nacelle
#

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.
vestal hound
#

How can you backreference a OneToOne relationship in django?
@limber laurel the name of the other model, by default

limber laurel
#

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

vestal hound
#

does the user have a profile?

#

I'm assuming you have something like:

class Profile(models.Model):
    user = models.OneToOneField('User')
uncut rover
#

guys, can i use django as backend and pair it with css, html and js on frontend?

cyan violet
#

yes

uncut rover
#

ok, thx for the info

pulsar ivy
#

Anyone in here knows how to connect postgresql db to flask app (and to create it)

native tide
#

do u know if it's posible to run django on apache

vestal hound
#

ok, thx for the info
@uncut rover you probably want DRF for that

#

django-rest-framework

uncut rover
#

To connect frontend with django backend?

native tide
#

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

uncut rover
#

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')
native tide
#

well does anyone know

#

or have any slight idea?

uncut rover
#

maybe install flask_test

native tide
#

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

uncut rover
#

Does anyone have an idea of my error?

left vigil
#

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?

uncut rover
#
django.core.exceptions.ValidationError: ['“1st bool field” value must be either True or False.']
#

what is this error???!!!!

lean saddle
#

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

native tide
#

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

left vigil
#

POST

native tide
#

Flask or django? @left vigil
do you have methods = ['GET','POST',] in the app route

left vigil
#

no framework

#

i created my own http and cgiservers

quick cargo
#

Sounds dangerous

#

whats your code

flint breach
#

if it changes the uri itself, im gueesing you're still passing some sort of arguments via a get method

#

"your own http"?

quick cargo
#

im guessing he means framework and server

left vigil
#

@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

quick cargo
#

i created my own http and cgiservers

#

That code

left vigil
#

its long

flint breach
#

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?

quick cargo
#

then link a github or send a paste bin

left vigil
#

oh no i do

#

i want it to refresh

quick cargo
#

we cant really guess what you have

left vigil
#

but not chagne the url

quick cargo
#

or dont have

left vigil
#

@quick cargo it's long code but contains private info ill change it and upload

flint breach
#

then you have to redirect back to the place where you sent the request from

left vigil
#

how?

flint breach
#

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 🤷‍♂️

left vigil
#

after the form is submitted is it up to the server to refresh the page or is it client side

quick cargo
#

client

#

well technically its a redirect the server sends back

#

and the client follows the redirect therefore reloading the page

left vigil
#

yeah like its weird since the server does suppose to redirect

#

but it doesnt

quick cargo
#

well we dont know you code so we cant really help much

left vigil
#

okkk ill upload it soon

native tide
#
          <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.

fickle fox
#

anyone has good tutorial on flask socket io, i have forum but i want to allow users to chat in private

glass sandal
#

I guess documentation is sufficient for your needs @fickle fox

#

I mean the official on

#

one*

rustic pebble
#

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

fickle fox
#

i am trying qwith it tho kinda confusing

glass sandal
#

I see

#

Guys I hate frontend dev and mostly CSS . What could I do to avoid it as much as I can?

formal shell
#

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

glass sandal
#

I see

#

@formal shell And you can pass it as a refrenc

#

refrence*

#

Like

#
return render(request,"site.html",{"class":Class})
fickle fox
#

use bootstrap?

formal shell
#

Fair enough @glass sandal

vernal vault
glass sandal
#

Don't waste money in Udemy @vernal vault

#

Youtube tutorials are sufficient

#

Or even the documentation

vernal vault
#

I'm doing one now and learning a ton I scraped page this morning

#

for 10 its not a big risk

glass sandal
#

You could've done this all with some yt vids

vernal vault
#

who would you recommend for Youtube

glass sandal
#

Or even documentation

vernal vault
#

I just found tech with tim

glass sandal
#

He has good tutos

#

And very great for beginners

#

But I myself rather getting used to documentations

vernal vault
#

I feel like I need to move to the next step get better working with objects from libraries

glass sandal
#

wdym by objects? classes?

vernal vault
#

yes and how they work in libraries

glass sandal
#

Oh

vernal vault
#

and getting better with docs

glass sandal
#

Well , a good site to learn basics of programming could be sololearn.com

#

And again , youtube tutorials

vernal vault
#

Thanks for the advice

#

much appreciated

glass sandal
#

yw

vernal vault
quick cargo
#

best tutorial for it hands down is corey

#

Goes over everything from getting started to logins to deployment

nocturne jetty
#

@teal fox We don't allow recruitement/unapproved advertising

teal fox
#

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

nocturne jetty
#

It's still recruitement

teal fox
#

recruitement?

#

How?

nocturne jetty
#

You're asking people to answer a form for you

teal fox
#

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?

mellow tide
#

It's advertising something @teal fox, there are other places for that (see: reddit)

teal fox
#

Sorry, im having hard time understanding what im advertising?

mellow tide
#

Listen, the mods have spoken. You can accept it, or not, but the conversation is over.

teal fox
#

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)

mellow tide
#

Take it up in meta if you have disagreements

teal fox
#

@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

mellow tide
#

Then see #rules, now you are just spamming untreated nonsense in a webdev channel

teal fox
#

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 😉

uncut rover
#

Guys does anyone have an idea for a good tutor for django 3?

nocturne jetty
#

@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

teal fox
#

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.

nocturne jetty
#

I still have the deleted message.

teal fox
#

@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.

nocturne jetty
#

Using modmail will allow you to talk to multiple staff members. We can work this out if you'd like.

uncut rover
#

youtube, but all of them are for django 2

#

and im using dj 3

bleak bobcat
#

@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

frank vine
uncut rover
#

did you do it, and do you find it helpful?

#

i hope it suits 3.1

#

?

frank vine
#

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.

bleak bobcat
#

There's no major breaking changes, mostly new async features

#

Can't wait for the async orm, that's the only async feature missing now