Home > Software engineering >  Import csv file using django - form doesn't appear
Import csv file using django - form doesn't appear

Time:04-02

I try to create a project and I need to import a csv file (Import csv file using django - Exception Type: DatabaseError)

I want to display the form on other page, not on home. But, when I create a new path, it didn't display me that specific view.

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
    })

main file - 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", {})

If I delete the above views (import, home), the template that I have for home.html and I remove from csvs.urls.py that 'import/' and remove the same thing from main file - urls.py, like this: path('', include('csvs.urls', namespace='csvs')), the form is displayed on the home page, in other case no.

I tried to change the last row from csvs/view.py and put 'import.html' and in the import.html to have the same code that it is in upload.html, but still nothing.

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%}

I really got stuck. I want to continue my project, but I don't know how to solve all of this. I am new with django, mongodb.

Thank you a lot for your time!

CodePudding user response:

Note

As far as I read your code, the problem in main-urls.py will be solved but you will have to decide to the import/ in either sales.urls or csvs.urls

In 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)
  • Related