Как получить значение в Django без POST или GET SIMILAR to FLASK request.args.get

как получить request.args.get в Django без изменения url.py и без передачи аргумента/переменной через def index(request, NO__EXTRA_VARIABLE_HERE) можно ли получить значение без POST или GET формы? Что я могу использовать для получения этого значения в Django?

Я пробовал: request.GET, но затем я получаю WSGI Error

или

Exception Value:    
Reverse for 'index' with keyword arguments '{'currency_type': 'usa'}' not found. 1 pattern(s) tried: ['\\Z']
<a href="{% url 'index' currency_type=item.currency_type %}" class="btn btn-outline-primary {{item.active}}" role="button">{{item.display_text}}</a>

КОД КОЛБЫ

index.html в приложении flask

<div class="card-footer text-center">
                <a class="btn btn-primary" href="{{ url_for('movie_details', movie_id=movie.id) }}">Show more</a>
</div>

 @app.route('/')
def homepage():

    selected_list = request.args.get('list_type', "popular")


    buttons = [
        {"list_type": "now_playing", "active": "", "display_text": "now playing"},
        {"list_type": "popular", "active": "", "display_text": "popular"},
        {"list_type": "upcoming", "active": "", "display_text": "upcoming"},
        {"list_type": "top_rated", "active": "", "display_text": "top rated"},
        {"list_type": "favorites", "active": "", "display_text": "favorites"},
    ]

    if selected_list not in ["now_playing", "upcoming", "top_rated","favorites"]:
        selected_list = "popular"""


    for button in buttons:
        if button['list_type'] == selected_list:
            button['active'] = 'active'

    if selected_list =="favorites":
        favorite_list = show_favorite()
        list_of_favorites = [get_single_movie(i) for i in favorite_list]
        if not favorite_list:
            flash("You dont have favorites movies", "alert-danger")
            return redirect(url_for("homepage"))
        return render_template("homepage.html",current_list=selected_list,buttons=buttons, list_of_favorites=list_of_favorites)

    else:
        movies = tmdb_client.get_movies(how_many=8, list_type=selected_list)
        return render_template("homepage.html",current_list=selected_list,buttons=buttons, movies=movies) 

Что я пытался сделать в Django:

def index(request):
    
    selected_currency_type = request.GET.get('currency_type', "usa")
    buttons = [
            {"currency_type": "usa", "active": "", "display_text": "USD"},
            {"currency_type": "euro", "active": "", "display_text": "Euro"},
            {"currency_type": "cny", "active": "", "display_text": "Chinese Yuan"},
            {"currency_type": "jpy", "active": "", "display_text": "Japanese Yen"},
        ]
    for button in buttons:
        if button['currency_type'] == selected_currency_type:
            button['active'] = 'active'

    currecies_list = load_currencies(selected_currency_type)
    context = {
                "user": request.user,
                "currecies_list": currecies_list,
                "buttons" : buttons,
                    }
    return render(request, "web/index.html", context)

вы можете создать запрос get таким образом:

<a href="{% url 'index' %}?currency_type={{item.currency_type}}"

другими словами: разделите генерацию url и параметров, то есть передавайте параметры не как часть пути, а как параметры после ?

Таким образом, параметр не является частью пути и не влияет на ваш url-шаблон в urls.py. Но параметры все еще находятся в объекте запроса и через него доступны в представлении.

Вернуться на верх