Sobes.tech
Junior — Middle

How can you set a URL for displaying views in a project?

sobes.tech AI

Answer from AI

In Python projects, especially when using web frameworks, URLs for displaying views are set through routing.

For example, in Django, URLs are defined in the urls.py file using the path or re_path functions, linking URLs to view functions or classes:

from django.urls import path
from . import views

urlpatterns = [
    path('home/', views.home_view, name='home'),
    path('articles/<int:id>/', views.article_detail, name='article_detail'),
]

In Flask, URLs are set using decorators @app.route above functions:

from flask import Flask
app = Flask(__name__)

@app.route('/home')
def home_view():
    return "Main page"

@app.route('/articles/<int:id>')
def article_detail(id):
    return f"Article {id}"

Thus, URLs are linked to specific functions or classes that handle requests and return responses.