Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop e-commerce independently with Vue+Django

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to develop e-commerce independently with Vue+Django". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the settings.py configuration file under myproject, replace the default sqllite3 database with our mysql database

# Database# https://docs.djangoproject.com/en/1.11/ref/settings/#databasesDATABASES = {'default': {' ENGINE': 'django.db.backends.mysql',' NAME': 'myproject',' USER': 'root',' PASSWORD': 'root',' HOST': '127.0.0.1,}}

And add app to the installed_apps list:

INSTALLED_APPS = ['django.contrib.admin',' django.contrib.auth', 'django.contrib.contenttypes',' django.contrib.sessions', 'django.contrib.messages',' django.contrib.staticfiles', 'myapp',]

4. In the models.py under the app directory, we simply write a model as follows:

#-*-coding: utf-8-*-from _ _ future__ import unicode_literalsfrom django.db import models# Create your models here.class Book (models.Model): book_name = models.CharField (max_length=64) add_time = models.DateTimeField (auto_now_add=True) def _ unicode__ (self): return self.book_name

There are only two fields, the title of the book book_name and the added time add_time. If no primary key is specified, django will automatically add a self-increasing id as the primary key.

5. We have added two APIs to views under the app directory. One is that show_books returns the list of all books (the json format data that can be recognized by the frontend through JsonResponse), and the other is that add_book accepts a get request and adds a book data to the database:

# Create your views here.@require_http_methods (["GET"]) def add_book (request): response = {} try: book = Book (book_name=request.GET.get ('book_name')) book.save () response [' msg'] = 'success' response [' error_num'] = 0 except Exception E: response ['msg'] = str (e) response [' error_num'] = 1 return JsonResponse (response) @ require_http_methods (["GET"]) def show_books (request): response = {} try: books = Book.objects.filter () response ['list'] = json.loads (serializers.serialize ("json") Books) response ['msg'] =' success' response ['error_num'] = 0 except Exception,e: response [' msg'] = str (e) response ['error_num'] = 1 return JsonResponse (response)

As you can see, with the help of ORM, our interface does not actually need to organize the SQL code on its own

6. Under the app directory, add a urls.py file and add the two new APIs to the route:

From django.conf.urls import url, include import views

Urlpatterns = [url (ringing addenda bookstores, views.add_book,), url (rushing showbooks bookswriting, views.show_books,),]

We also need to add the urls under app to the urls under project to complete the routing:

From django.conf.urls import url, includefrom django.contrib import adminfrom django.views.generic import TemplateViewimport myapp.urlsurlpatterns = [url (r'^ admin/', admin.site.urls), url (r'^ api/', include (myapp.urls)), url (r'^ $', TemplateView.as_view (template_name= "index.html")),] the content of "how to develop e-commerce independently with Vue+Django" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report