空路径与这些都不匹配,搞不清楚。

问题描述 投票:-1回答:1

safe}}

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from expense.views import expense_upload


urlpatterns = [
    path('admin/', admin.site.urls),
    path('upload-csv/', expense_upload, name="expense_upload")
]

{% endfor %} {% else %} {{order}} {% csrf_token %} Upload a file Only accepts CSV files Upload {% endif %} {% for expense in expenses %} {{expense.MONTH}} {% endfor %}

import csv, io
from django.shortcuts import render
from django.contrib import messages
# Create your views here.

#parameter named request
def expense_upload(request):
    #declaring template
    template = "expense_upload.html"
    data = Expense.objects.all()

#prompt is a context varibale that can have different values depending on their context
    prompt = {
        'order': 'Order of the CSV should be Month, Date, Description, Category, Withdrawal, Deposit, Paid_by, Purpose',
        'expenses': data
    }

#Get request returns the value of the data with the specified key.
    if request.method == "GET":
        return render(request, template, prompt)

    csv_file = request.FILES['file']

    #lets check if it is a csv file
    if not csv_file.name.endswith('.csv'):
        messages.error(request, 'Please upload a valid csv file')

    data_set = csv_file.read().decode('UTF-8')

    io_string = io.StringIO(data_set)
    next(io_string)
    for column in csv.reader(io_string, delimiter=',', quotechar="|"):
        _, created = Expense.objects.update_or_create(
            MONTH = column[0],
            DATE = column[1],
            DESCRIPTION = column[2],
            CATEGORY = column[3],
            WITHDRAWAL = column[4],
            DEPOSIT = column[5],
            PAID_BY = column[6],
            PURPOSE = column[7]
        )
        context = {}
        return render(request, template, context)

urls.py ''' from django.contrib import admin from django.urls import path from django.conf import settings from django.conf.urls.static import static from expense.views import expense_upload ...The empty path can not be reached because u did not specify any empty path in urls.py instead of path('upload-csv'''
views.py'''
django django-urls
1个回答
0
投票

{{message', expense_upload, name="expense_upload")尝试使用path('', expense_upload, name="expense_upload")。这样当你打开网站的时候,你会看到这个页面是第一个页面。

© www.soinside.com 2019 - 2024. All rights reserved.