使用 django 导入 csv 文件 - 表单不出现
Import csv file using django - form doesn't appear
我尝试创建一个项目,我需要导入一个 csv 文件 (Import csv file using django - Exception Type: DatabaseError)
我想在其他页面上显示表格,而不是在主页上。
但是,当我创建一个新路径时,它并没有显示那个特定的视图。
csvs/urls.py
from django.urls import path
from .views import upload_file_view
app_name='csvs'
urlpatterns =[
path('import/', upload_file_view, name='upload-view')
]
csvs/views.py
from django.shortcuts import render
from .forms import CsvModelForm
from .models import Csv
import csv
from django.contrib.auth.models import User
from sales.models import Sales
def upload_file_view(request):
form = CsvModelForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form = CsvModelForm()
obj= Csv.objects.get(activated=False)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i==0:
pass
else:
#row = "".join(row)
#row = row.replace(";"," ")
#row=row.split()
#print(row)
#print(type(row))
date = row[1]
user = User.objects.get(username=row[0])
Sales.objects.create(
date=date,
product= row[2],
user=user,
)
obj.activated=True
obj.save()
return render(request, 'upload.html', {
'form': form
})
主文件 - urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from sales.views import import_view, home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('import/', import_view, name="import"),
path('', home_view, name="home"),
path('import/', include('csvs.urls', namespace='csvs')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
sales/views.py
from django.shortcuts import render
# Create your views here.
def import_view(request, *args, **kwargs):
return render(request, "import.html", {})
def home_view(request, *args, **kwargs):
return render(request, "home.html", {})
如果我删除上面的视图(导入、主页),我为 home.html 准备的模板,然后从 csvs.urls.py 中删除'import/' 并从 主文件中删除相同的内容 - urls.py,如下所示:path('', include('csvs.urls', namespace='csvs')), 表格显示在首页, 其他情况下没有.
我尝试更改 csvs/view.py 的最后一行并放入 'import.html' 并在 import.html 中具有相同的代码它在 upload.html 中,但仍然没有。
upload.html
{% extends 'base.html' %}
{% block title %}Import{% endblock %}
{% block content%}
<h1>Import</h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Confirm</button>
</form>
{% endblock%}
我真的卡住了。我想继续我的项目,但我不知道如何解决所有这些问题。我是 django 的新手,mongodb.
非常感谢您的宝贵时间!
备注
据我阅读你的代码,main-urls.py 中的问题将得到解决,但你必须决定 sales.urls
或 [=13] 中的 import/
=]
在main-urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from sales.views import import_view, home_view
othor_urls = [
# This is for all csvs urls
path('csvs/', include('csvs.urls')),
# This is for all sales urls
path('sales/', include('sales.urls')),
]
urlpatterns = [
path('admin/', admin.site.urls),
# Remove import/ as it is move to other_urls
# path('import/', import_view, name="import"),
path('', home_view, name="home"),
path('import/', include(othor_urls)),
# Also remove this whihc is moved to other_urls
# path('import/', include('csvs.urls', namespace='csvs')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我尝试创建一个项目,我需要导入一个 csv 文件 (Import csv file using django - Exception Type: DatabaseError)
我想在其他页面上显示表格,而不是在主页上。 但是,当我创建一个新路径时,它并没有显示那个特定的视图。
csvs/urls.py
from django.urls import path
from .views import upload_file_view
app_name='csvs'
urlpatterns =[
path('import/', upload_file_view, name='upload-view')
]
csvs/views.py
from django.shortcuts import render
from .forms import CsvModelForm
from .models import Csv
import csv
from django.contrib.auth.models import User
from sales.models import Sales
def upload_file_view(request):
form = CsvModelForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form = CsvModelForm()
obj= Csv.objects.get(activated=False)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i==0:
pass
else:
#row = "".join(row)
#row = row.replace(";"," ")
#row=row.split()
#print(row)
#print(type(row))
date = row[1]
user = User.objects.get(username=row[0])
Sales.objects.create(
date=date,
product= row[2],
user=user,
)
obj.activated=True
obj.save()
return render(request, 'upload.html', {
'form': form
})
主文件 - urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from sales.views import import_view, home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('import/', import_view, name="import"),
path('', home_view, name="home"),
path('import/', include('csvs.urls', namespace='csvs')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
sales/views.py
from django.shortcuts import render
# Create your views here.
def import_view(request, *args, **kwargs):
return render(request, "import.html", {})
def home_view(request, *args, **kwargs):
return render(request, "home.html", {})
如果我删除上面的视图(导入、主页),我为 home.html 准备的模板,然后从 csvs.urls.py 中删除'import/' 并从 主文件中删除相同的内容 - urls.py,如下所示:path('', include('csvs.urls', namespace='csvs')), 表格显示在首页, 其他情况下没有.
我尝试更改 csvs/view.py 的最后一行并放入 'import.html' 并在 import.html 中具有相同的代码它在 upload.html 中,但仍然没有。
upload.html
{% extends 'base.html' %}
{% block title %}Import{% endblock %}
{% block content%}
<h1>Import</h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Confirm</button>
</form>
{% endblock%}
我真的卡住了。我想继续我的项目,但我不知道如何解决所有这些问题。我是 django 的新手,mongodb.
非常感谢您的宝贵时间!
备注
据我阅读你的代码,main-urls.py 中的问题将得到解决,但你必须决定 sales.urls
或 [=13] 中的 import/
=]
在main-urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from sales.views import import_view, home_view
othor_urls = [
# This is for all csvs urls
path('csvs/', include('csvs.urls')),
# This is for all sales urls
path('sales/', include('sales.urls')),
]
urlpatterns = [
path('admin/', admin.site.urls),
# Remove import/ as it is move to other_urls
# path('import/', import_view, name="import"),
path('', home_view, name="home"),
path('import/', include(othor_urls)),
# Also remove this whihc is moved to other_urls
# path('import/', include('csvs.urls', namespace='csvs')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)