Saturday, July 31, 2021

great repository for Tailwind css and Django

I am a big fan of Tailwind CSS. It is a great companion of Styled Component.

you should be able to find lots of useful resource for Tailwind CSS in this GitHub

https://github.com/aniftyco/awesome-tailwindcss


Tailblocks Ready to use Tailwind CSS Block

It is a great place to find the suitable CSS block to copy and paste in your project

https://tailblocks.cc/


a site with all Django package

https://djangopackages.org/


Save All Resources

https://chrome.google.com/webstore/detail/save-all-resources/abpdnfjocnmdomablahdcfnoggeeiedb/related?hl=en


Virtual Environment for django and python development

https://virtualenvwrapper.readthedocs.io/en/latest/


how to move the side bar to the right in visual studio code?

 I had work for Visual Studio since 2003 version. it is default by the side bar and explorer are located in the right hand side. However the side bar is shown in the left hand side of the visual studio code. I am really not used to be with the change of the location.

it is very easy to switch it to the one which is the right hand side. just right click  to the side bar, then select Move Side Bar Right menu item from the Menu.













Now it is back to the layout that I work most of the time.




how to setup template folder for Django development?

 there are two ways to setup the template folder for django development.

first we can setup the path that Django framework can automatically recognize it..

under the component folder, we have to create the a template folder, then create a folder with name matching the component name.

for example we have a lead component, then the structure of the project should look like as following








in the view.py, we can implement with the code snippet below

from django.shortcuts import render

# Create your views here.

def home_page(request):
    return render(request"leads/home_page.html")

second. we can modify the DIRS attribute in  TEMPLATES setting section in the settings.json to map to the templates path.

TEMPLATES = [
    {
        'BACKEND''django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS'True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

then we only have to create a templates folder under the current component folder. we can add the template html in the template folder.







in the view.py we use the template page directly.

def home_page(request):
    return render(request"home_page.html")








Friday, July 30, 2021

how to fix "Import "django.contrib.auth" could not be resolved from sourcePylance" in Django application development?

 when I try to import a library to the project. I spot a curly underline for the library that i just imported.

I hovered the mouse to the underneath, the warming message as following

    "Import "django.contrib.auth" could not be resolved from sourcePylance"


the root cause of this issue is related to the VS Code workspace configuration. we have to add this line to get ride of the error message.

{
    "python.pythonPath""/venv/bin/python",
}

Since I did not have the .vscode folder when I setup my django project with command line window.

I have to manually create a .vscode folder under the project and add the settings.json file inside it.

I put the above setting in the settings.json file.  The warming message finally resolved.










update: if the above solution still doesn't work

you can change the python interpreter for the current project by doing the following

  • use Ctrl-Shift-p to open command palette
  • type python interpreter in the command palette
  • select Enter interpreter path
  • find the python.exe file under your venv\scripts folder


How can we set restrictions on page in flask app?

 if we want to the user had to login to view the page.

we can use this decorator   @login_required to enforce only login user can access the page.

@app.route('/private')
@login_required
def private_page():
purchase_form = PurchaseItemForm()
if request.method == "POST":
purchased_item = request.form.get('purchased_item')
p_item_object = Item.query.filter_by(name=purchased_item).first()
if p_item_object:
if current_user.can_purchase(p_item_object):
p_item_object.buy(current_user)
flash(f"Congratulations! You purchased {p_item_object.name} for {p_item_object.price}$", category='success')
else:
flash(f"Unfortunately, you don't have enough money to purchase {p_item_object.name}!", category='danger')
return redirect(url_for('private_page'))
if request.method == "GET":
items = Item.query.filter_by(owner=None)
for item in items:
print(item.name)
return render_template('market.html', items=items, purchase_form=purchase_form)

Monday, July 26, 2021

how to implement dynamic routing in Flask

 When we want to pass into a variable on routing. we can use the following code to pass the variable <routeVariable> in the route path.

@app.route("/mypage/<username>"

def  my_page(username):

        return f"<h1>Hello {username}'s page<h1>"


the output is 

hello dan's page

how to force a flask web app to run on specific port in local development?

 when I execute flask run to launch the flask web app in my laptop. I usaully encoutered an error as following.

    "OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions " 

since the windows firewall had block this port.

I can assign the port during the launch to void this issue. i assing 300 for this app.

flask run -h localhost -p 3000