Hey guys so I been looking into making a site and was going to learn php for it but after a lot of research I saw how I can use Django for the backend of the site so I already started creating a site with HTML/css/js before I decide to use Django. Is there any possible way you can connect HTML/css to Django or do I need to re write everything in Django. I did look at some tutorials and saw how all the HTML was in django and I also did try looking it up too but couldn't find the right answer I'm looking for. If anyone could tell me I'll be very appreciated!! Thanks!
You can connect it. Django is a backend framework, all it does in the frontend is print data.
Read about templates in Django. Templates are basically html files with dynamic content.
Take the template my_template.html:
My first name is {{ first_name }}.
You render it like this:
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', {'first_name': 'Cesar'})
It will output:
My first name is Cesar.
Make sure to read the whole intro tutorial to Django.
Note: There are many ways to use a template, this was just a little example.
All you need to move from your old project to new one created with django-admin startproject are:
HTML templates adapted to django template language configured as described here: https://docs.djangoproject.com/en/1.10/topics/templates/
css/img/js files to static folder
After it you need to replace old href attributes to something like <a href="{% url "Index" %}"> and insert correct paths (includes static url) of css/img/js files to hmtl
Related
I have built a complete front-end portfolio using HTML, CSS, and js. There is a a email contact form in the webpage and I want to send an email using Django through that contact form. when I tried to integrate it the index page is rendering but the CSS file is not accessible and all the design is disorganized.
In this way my project files are organised:
This is the actual design:
But after rendering through Django it looks like this:
you just need to address your static files, you can visit this Django doc.
You have your templates folder a bit messy. It actually needs to contain only HTML pages. All the other files move to a 'Static' folder. Then in settings.py you will need to specify this folder as STATIC_URL = '/static/'. This is what would I do at first.
Problem:
I completed the Ruby on Rails "Hello World" tutorial here which walks you through how to build a blog. Now, I want to see if I can embed the work that I've done into an html file.
What I've looked into:
However, I am not exactly sure how to go about that. I looked into embedded ruby and found a good tutorial/explanation on Tutorials Point and some other sites on Google. Those examples show how to write ruby code within the file though. That seems great if I'm only writing a few lines, but I want to incorporate an entire project.
I also found a link on writing templates, but that didn't seem like what I was looking for either.
Question:
Is there a way I can add my blog to a static html/css site that I've already created?
Something like <% link railsblog %>? Or is there any other way to incorporate the project on an html page?
Thanks for any advice!
So you have a Static Html-Css website. You want to add a blog to it. Right?
The thing is, a Ruby on Rails project (eg: your blog) is not something 'additional' that you can add to a website.
It is a very powerful framework that allows you to build an ENTIRE website within the project.
Once you have a Rails project (your blog) running, you can put all your other existing static html, css, js files into the Rails PUBLIC folder.
Now if start up your server ( run rails server ), and try to access your other pages, you should see them. eg: ( localhost:3000/my-page-name.html )
Now to get localhost:3000 to point to your actual homepage.
In your 'routes.rb' file, add route:
root 'main#index'
Create a new Controller file in controllers:
class MainController < ApplicationController
def index
redirect_to '/index.html'
end
end
You'll have to learn a couple of things about Rails if you're planning to move your entire website to Rails or if you're curious.
This should help you get started.
Or if you're not looking to learn an entire framework for a blog, try this.
Good luck!
I think the solution is to create a partial html.erb file, for intance:
_fileName.html.erb
then you can put inside your html code and render by calling:
<%= render 'layouts/fileName' %>
You can always render a file (e.g static form public folder) or render a view if it has a respective controller/action. Remember that to render a view you need right routing in routes.rb except rendering partials file which should start with underscore e.g _partial.html.erb
If you want to call some resource embeded in other web site you can always use but it has nothing in common with rails.
I made a website with Twitter bootstrap and the design is finished in terms of all the HTML and CSS.
Now I need to add functions like making an account and blogging. I decided to use Ruby on Rails to do this, but I have no idea where to put the files and how to connect them.
So far I have figured out that I want to put them in the "views" folder and have it say, for example, "index.html.erb" but I don't know if that goes into another folder in "views" or how any of that works.
I also don't know where to put all the CSS and JavaScript incorporated with bootstrap.
Following Rails conventions, views should go in a dir that mirrors the model name (plural), which also aligns with the controller name. i.e. model = user, views dir = users, controller = users_controller.rb. You should read through at least the first few Rails guides; esp this layout and rendering one:
RE the bootstrap portion of this question, just use the bootstrap-sass gem and follow the instructions for including it in your application.scss and application.js.
I finished my Ruby on Rails project and I get extra credit for making it "look good." I made an HTML template with some images and css styling on my local machine (not the RoR server). I've tested it out in plain HTML and it looks good.
I can't figure out how to incorporate it into my RoR project though. I thought i'd be able to create a directory in my views, called "images" to hold the images for my template.
Before transferring all of the code for my RoR to the tags for my template, I wanted to make sure the images were in the right location. I added to my index.html.erb file, but it won't display. I tried moving that images directory to a few other directories and tried again, same thing!
I thought it would be easy to incorporate a template haha but now I'm thinking not?
Is there a way to simply do this like an ordinary HTML website?
There are two main options for this...
You can use the asset pipeline and serve up assets from a location like app/assets... So, app/assets/images.
You can put them in public/images and just serve them up that way as well.
If you feel like learning a bit more, I'd dig into the asset pipeline. If you're just ready to be done, public/images for a small project should work just fine. :)
I would like to know how to import an HTML file into an other HTML file.
Both files refer to a lot of different files, so I would prefer to keep them into two different folders instead of copying one file into the other.
For now, I'm developing locally, with Django.
{% include 'file.html' %} should work
I've not used Django before, but they have documentation on their template language here.
https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance
I am not exactly sure of the question but this might help.
Including pages with only HTML:
<!--#include virtual="pagetoinclude.html" -->